Search code examples
c#typescom-interop

Type.InvokeMember - DISP_E_MEMBERNOTFOUND


Type.InvokeMember is failing with error cannot find member DISP_E_MEMBERNOTFOUND despite the member being very definitely there.

The Microsoft Scripting Runtime familiar to VB script writers has a Dictionary class but I am failing to run a simple late bound call. Here is MCRE. I have yet to start marshalling the return argument.

    static void Main(string[] args)
    {
        Type type = Type.GetTypeFromProgID("Scripting.Dictionary");
        Object com_obj = Activator.CreateInstance(type);

        Object[] countArgs = new Object[1];
        string msg;
        BindingFlags invokeAttr = BindingFlags.InvokeMethod;
        try
        {

            type.InvokeMember("Count", invokeAttr, null, com_obj, null);
        }

        catch (Exception ex)
        {
            msg = ex.Message + ":" + ex.InnerException.Message;
            Debug.WriteLine(msg);
        }


        Console.ReadKey();
    }

Solution

  • Try to use BindingFlags invokeAttr = BindingFlags.GetProperty; instead. Alternativly you could use C#s dynamic to reduce the unreadable code to a minimum.

    Type type = Type.GetTypeFromProgID("Scripting.Dictionary");
    dynamic com_obj = Activator.CreateInstance(type);
    var count = com_obj.Count;