Omar Omar - 1 year ago 71
C# Question

Default Public Readonly Property in a VB assembly is not appearing when referenced in c#

I am having problems when I try to use a property from a VB assembly in a C# App. The VB assembly was created in .NET 1.1 and the App where I am trying to use it is a .NET 4.5 in C#. I reviewed the code of the VB assembly and noted that the property in the assembly that is not appearing for use when I instantiate the object in C# App is one declared as

Default Public ReadOnly Property
. If I remove the
keyword then the property appears in the C# object. But the problem here is that the dll is used in a lot of other applications created in VB and in those apps this problem does not exist. I don't have the possibility to change this dll only for my code.

Here is an example of what is happening to me:

Code from VB Assembly

Default Public ReadOnly Property MyProperty(ByVal value As String) As String
End Get
End Property

And in a C# instantiated object this property never appears until I remove the
keyword from it.

Answer Source

I created a test project in C# and ran the following:

        var foo = new Class1();
        Console.WriteLine(foo.MyProperty("Hello")); // Compile-time error
        Console.WriteLine(foo.get_MyProperty("Hello")); // Compile-time error: 'Test1.Class1.this[string].get': cannot explicitly call operator or accessor
        Console.WriteLine(foo.GetType().GetProperty("MyProperty").GetGetMethod().Invoke(foo, new object[]{"Hello"}));   // Works
        Console.WriteLine(foo["Hello"]);            // Works

The normal way to access an indexer in C# is the last line, and that works. Intellisense doesn't show a MyProperty property, and trying to call it gives a compile-time error. But it looks like I can access it through reflection.

The documentation for using indexers in C# shows code that uses an attribute to specify an indexer name for other languages, so I'm guessing that C# just doesn't support calling an indexer in this manner.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download