简体   繁体   中英

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. The DLL is written using 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

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

How would I access this same property using 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...

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...

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#. However, by adding the Default keyword to a VB.NET Property, this changes how the Property will be exposed.

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#. So you would access this Property like...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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