POWERSHELL代码:
[Reflection.Assembly]::LoadFile("C:\Users\Administrator\Desktop\xxxx\DLL.dll")
$DLL = New-Object DLL.DLL("test","test","test","test") #This works fine and creates an instance of the object.
echo $DLL.Method() #This outputs perfectly fine. ('butter' or whatever is comes back because its a getter method)
$DLL.Method("TestString") #This fails saying DLL.DLL does not contain a method named Method (or whatever my method name is!!) This is a setter method. Its supposed to set some variable and the other one returns it.
这是我的C#代码:
namespace DLL
{
public class DLL
{
private string _strRandomString
//*************************************************************************
//CONSTRUCTOR
//*************************************************************************
//Not writing code for constructor because it works 100% properly
//*************************************************************************
// public properties
//*************************************************************************
public bool Method
{
get { return this._strRandomString; }
set { this._strRandomString = value; } //!!! Unable to set the string but I can get it back!
}
}
我尝试了很多事情,例如制作一个字符串,然后将其传递到我的方法中,执行类似$ DLL :: Method(“ string”)等的事情。
我无法设置它!只获得价值!
错误是:
Method invocation failed because [DDL.DLL] does not contain a method named 'Method'.
At C:\Users\Administrator\Desktop\xxxx.ps1:31 char:1
+ $DLL::Method($test);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
getter和setter方法underly性质,并与属性访问语法(.<name>
)你从来没有使用()
-既不让也不上设置。
$DLL.Method # get the - paradoxically named - "Method" property
$DLL.Method = 'madness' # set the "Method" property, assuming it isn't read-only
注意:这意味着$DLL.Method()
您的代码永远无法使用。
另外-但更冗长-你可以使用方法的语法与get_
和set_
前缀,在这种情况下,您需要 ()
:
$DLL.get_Method() # same as: $DLL.Method
$DLL.set_Method('madness') # same as: $DLL.Method = 'madness'
当使用存取方法是不太方便,它们的用途,有时需要:当给定实例具有ETS属性即阴影的.NET类型的真实属性,只有访问方法提供访问后者; 例如:
$xmlDoc = [xml] '<Foo Name="bar" />'
$xmlDoc.Foo.Name # ETS property that contains the Name *attribute*'s value.
$xmlDoc.Foo.get_Name() # true [xml] property that contains the *element*'s name.
示范:
这是一个自成一体的示例,展示了上述内容。
注意:
Method
已重命名SomeProperty
以避免混淆。string
与_strRandomString
后备变量的类型匹配。Add-Type @'
namespace DLL
{
public class DLL
{
private string _strRandomString = "random?";
public string SomeProperty
{
get { return this._strRandomString; }
set { _strRandomString = value; } //!!! Unable to set the string but I can get it back!
}
}
}
'@
$obj = [DLL.DLL]::new()
$obj.SomeProperty # get the property
$obj.SomeProperty = 'new value' # set the property
$obj.SomeProperty # get the updated value
$obj.set_SomeProperty('even newer value') # use the setter method
$obj.get_SomeProperty() # use the getter method
以上收益:
random?
new value
even newer value
顺便说一句:请避免在PowerShell命令中使用伪方法语法:
New-Object SomeType(arg1, ...)
采用
New-Object SomeType [-ArgumentList] arg1, ...
PowerShell cmdlet和函数的调用类似于shell命令,而不是方法。
也就是说,没有括号周围参数列表,与我ndividual参数相隔的空格,没有,
。
在的情况下New-Object
,您正在(隐式地)将单个数组值参数传递给参数-ArgumentList
(其元素以-,
分隔)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句