0

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
Community
  • 1
  • 1
Zee
  • 1,780
  • 3
  • 16
  • 27
  • 4
    Keyword: `reflection` – EZI Jun 15 '15 at 19:34
  • @crashmstr I literally linked the question you refer to and specify how it does not accomplish what I need. – Zee Jun 15 '15 at 19:36
  • Maybe something like `Object myVar = MyClass.MyProperty["a property name"];` – maniak1982 Jun 15 '15 at 19:37
  • 2
    @zee so `MyClass.get_MyProperty(name);` does not return an `Object`? (e.g. `var myVar = MyClass.get_MyProperty("a property name");` – crashmstr Jun 15 '15 at 19:39
  • 3
    @Zee: Your answer is right there in the linked question. Annoyingly they choose to name their property `AsString` which might be what's confusing you. But you should be able to type something like `var foo = MyClass.get_MyProperty("a property name")`. C# just converts those into regular function calls. – Matt Burland Jun 15 '15 at 19:41
  • @crashmstr I updated the question. – Zee Jun 15 '15 at 19:52
  • @crashmstr The other thing I notice that is different from the suggested duplicate question is that.. one of the answers states I should see the "get_MyProperty" method with intellisense. I do not see this method listed by intellisence. Perhaps this is part of the issue. The property is public though so I'm not sure why I would not be able to access it. – Zee Jun 15 '15 at 19:59
  • make sure your C# app has a reference to the VB DLL, and perhaps that the bitness matches. If the DLL is AnyCPU and your app is x86 it may not find it. I just did a quick test and it works perfectly (and exactly as the dupe indicates) . – Ňɏssa Pøngjǣrdenlarp Jun 15 '15 at 20:29
  • 1
    Something is not right with your example. Your property is not static. But you are trying to access it as if it was. In fact, your VB.NET example that you say worked, also makes this mistake. Are you sure your examples are accurate? Or is your property in fact static (or Shared in VB.NET lingo)? – sstan Jun 15 '15 at 21:11
  • also just tried to replicate the issue ; but everything works fine (as stated by Plutonix) either using a Shared property or using an instance property and accessing it from an instance of MyClass (as hinted by sstan) – Sehnsucht Jun 15 '15 at 22:28
  • Apologies everyone - My example was indeed incomplete. I have updated the quest (property was really a default property). I guess the default keyword does not expose the property at a method - It instead is exposed in C# as an indexer. The property is accessed in C# with an indexer on the class - MyClass["PropertyParameterValue"]; – Zee Jun 16 '15 at 14:13
  • Thank you all for your help – Zee Jun 16 '15 at 14:27

1 Answers1

0

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"];
Zee
  • 1,780
  • 3
  • 16
  • 27