I am developing a .dll in C# that makes extensive use of DynamicObject.

The .dll in other C# projects works fine, however in VB.NET TryGetMember doesn't seem to be called. Instead TryInvokeMember is called even if the member is a property.

Here is an example class from my .dll

namespace MyNamespace
{
    class MyDynamicClass : DynamicObject
    {
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            //Do stuff with property
        }

        public override bool TryInvokeMember(InvokeMemberBinder, object[] args, out object result)
        {
            //do stuff with function
        }
    }
}

In a C# project everything works as expected:

class Program
  {
    static void Main(string[] args)
    {
        dynamic myObj = new MyNamespace.MyDynamicClass();
        var getm = myObj.GetMember; //this will call TryGetMember
        var invokem = myObj.InvokeMember(); //this will call TryInvokeMember
    }
  }

In VB.NET, both lines result in call to TryInvokeMember

Sub Main()
    Dim myObj = New MyNamespace.MyDynamicClass()
    Dim getm = g.GetMember 'calls TryInvokeMember
    Dim invokem = g.InvokeMember() 'calls TryInvokeMember
End Sub

I can solve my problem by simply calling TryGetMember at the end of TryInvokeMember if it fails to call it as a function, however since I have never worked with VB.NET before, I wanted to make sure this workaround is necessary and I am not doing something wrong.

0 Answers