locked
Using reflection with a COM object created from ProgID RRS feed

  • Question

  • Hi, my task is to get signature information for a method of a COM object the latter created from ProgID.

    The following code prints "true" in console which means that no method "Multiply" exists:

    Type comType = Type.GetTypeFromProgID("FATestCOM.FATest.1", true);
    object obj = Activator.CreateInstance(comType);
    MethodInfo mInfo = obj.GetType().GetMethod("Multiply", BindingFlags.Public | BindingFlags.Instance);
    Console.WriteLine(mInfo == null);
    

    But at the same time the following code succeeds because it prints the expected value (12) in console:

    Type comType = Type.GetTypeFromProgID("FATestCOM.FATest.1", true);
    object obj = Activator.CreateInstance(comType);
    object[] args = new object[] { "3", "4" };
    object retValue = obj.GetType().InvokeMember("Multiply", BindingFlags.InvokeMethod, null, obj, args);
    Console.WriteLine(retValue);
    

    And if try to list all methods of the "obj" object, I will not see "Multiply" method among them; the following code

    Type comType = Type.GetTypeFromProgID("FATestCOM.FATest.1", true);
    object obj = Activator.CreateInstance(comType);
    MethodInfo[] mis = obj.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod);
    foreach (MethodInfo mi in mis)
    {
      Console.WriteLine(mi.Name);
    }
    

    produces the following console output:

    GetLifetimeService
    InitializeLifetimeService
    CreateObjRef
    ToString
    Equals
    GetHashCode
    GetType
    

    I guess the reason is that I create COM object via "late binding" - from its ProgID. I can suppose that my problem will go away if I add reference to that COM DLL. However, I cannot do that: creating arbitrary COM object from its ProgID is the limitation of my task. So, my question is: how can we get signature information for a method of a COM object the latter created from ProgID?

    Friday, August 20, 2010 8:35 AM

Answers

  • You can't use Reflection for that. You have to use the corresponding old COM APIs, ITypeInfo and friends. Assuming the object you're creating implements IDispatch, calling IDispatch.GetTypeInfo is a good start.

     


    Mattias, C# MVP
    • Marked as answer by Comanche Wednesday, August 25, 2010 6:21 AM
    Friday, August 20, 2010 3:15 PM

All replies

  • You can't use Reflection for that. You have to use the corresponding old COM APIs, ITypeInfo and friends. Assuming the object you're creating implements IDispatch, calling IDispatch.GetTypeInfo is a good start.

     


    Mattias, C# MVP
    • Marked as answer by Comanche Wednesday, August 25, 2010 6:21 AM
    Friday, August 20, 2010 3:15 PM
  • Hi Comanche,

    I'm writing to check the issue status, is Mattias' reply helpful for your issue? please feel free to let us know if you have any concern.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, August 24, 2010 10:08 AM
  • Yes, Mattias helped me, thank you - I was far from Internet so couldn't reply in time, sorry.
    Wednesday, August 25, 2010 6:21 AM