使用powershell,我正在导入C#DLL。我似乎只能调用实例的getter方法,而不能调用setter方法。

用户11989986

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
mklement0

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我应该使用构造函数getter和setter方法?

接口不能调用自我方法

如何从getter或setter调用异步方法?

Java反射-如何调用getter / setter方法?

为什么我的C#DLL中的方法丢失了?

如何获取正在调用我的方法的类名?

从经典ASP调用.Net C#DLL方法

如何在Python中将所有方法调用委派给C#DLL

我可以使实例方法不能作为静态方法调用吗?

从哲学上讲,我应该从getter或setter内调用方法吗?

如何在电子App中调用C#dll方法?

Powershell可以从外部C#dll调用子类

无法从C#DLL进行http调用

如何从Delphi调用C#dll方法时解决错误外部异常E0434352

当我对某个方法使用Invoke时,我不能再调用它了?

Ruby-实例方法:为什么我可以不使用self而使用getter,而只能使用self进行setter

我不能以类的形式从类中调用我的方法

在C#dll中使用平台调用

为什么我的setter方法没有被调用?

使用反射从C#Dll调用C#方法

我有我的类的方法指针数组,但我不能调用我的方法。代码如下

Kotlin - 我们如何使用 getter 和 setter 访问私有财产?访问方法是否在内部调用?

为什么我不能使用类的实例调用 Java 类的公共方法?

我正在尝试从运行文件调用模型方法

使用 getter 和 setter 调用实例属性好不好?

避免在 C# 使用不能为 null 的参数调用我的方法时发出警告

如何从线程正在使用的对象实例调用方法?

我无法调用 C# 中的方法正在使用

我无法使用 setter 和 getter 设置和调用值