简体   繁体   English

使用C#,如何使用参数访问VB类的默认属性的getter / setter?

[英]Using C#, how can I access the getter/setter of a VB class's default property with parameter?

I'm using a DLL, which I cannot change. 我正在使用DLL,无法更改。 The DLL is written using VB.NET. DLL是使用VB.NET编写的。 There is a class, which contains properties such as: 有一个类,其中包含以下属性:

Default Property MyProperty(Name As String) As SomeObject
     //getter & setter
End Property

Using VB.NET, I would access these values like this 使用VB.NET,我将像这样访问这些值

Dim myVar As String = MyClass.MyProperty("a property name").Value

How would I access this same property using C#? 如何使用C#访问同一属性? I found an answer for a similar situation here . 我在这里找到了类似情况的答案。 However, I need to apply the concept to something other than a string. 但是,我需要将此概念应用于字符串以外的其他东西。

So far I have tried 到目前为止,我已经尝试过

String returnValue = MyClass.get_MyProperty("a property name").Value

This causes a build error: 这会导致生成错误:

'MyClass.this[string].get': cannot explicitly call operator or accessor

In VB.NET, a Property with parameters such as... 在VB.NET中,具有参数的属性,例如...

Public Class MyClass
    Public Property MyProperty(param as String) as String
        //Getter
        //Setter
End Class

can be accessed in C# using an assembly reference to the VB.NET DLL like... 可以使用对VB.NET DLL的程序集引用在C#中进行访问,例如...

MyClass classInstance = new MyClass();
classInstance.set_MyProperty("param", "value");
String var = classInstance.get_MyProperty("param");

The VB.NET Property is exposed as a method in C#. VB.NET属性作为C#中的方法公开。 However, by adding the Default keyword to a VB.NET Property, this changes how the Property will be exposed. 但是,通过将Default关键字添加到VB.NET属性,可以更改该属性的显示方式。

Public Class MyClass
    Default Property MyProperty(param as String) as String
        //Getter
        //Setter
End Class

When the Default keyword exists, the Property is then exposed as an indexer in C#. 当存在Default关键字时,该属性随后在C#中公开为索引器。 So you would access this Property like... 因此,您可以像以下方式访问此属性:

MyClass classInstance = new MyClass();
classInstance["param"] = "value";
String var = classInstance["param"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM