mercredi 17 juin 2020

How to use reflection on a COM object exposed through a RCW?

I used the Type Library Importer (TlbImp.exe) to generate an interop assembly for interfacing with a COM object. Something I am trying to do is build an expression tree that calls a method on a COM object. I want to compile the expression tree into a lambda and cache it.

The reason I want to do this is because in this COM object, there are many similar interfaces where the only thing that changes is the parameter type. For example, there is an interface for IFooDouble, IFooInt, IFooString, IFooLongInt, etc. Then each one will define a method SetValue(T value) where T will be either double, int, string, etc., depending on the interface. Here I would compile an expression tree into an Action<IFooBase, T> lambda and cache it.

The generated interop assembly contains strongly typed interfaces to the SetValue method, but I cannot find a way to get a reference to it's MethodInfo object through reflection. Since the wrapper type is System.__ComObject, I can call InvokeMember and do it that way. However, I am wondering if it will be much slower than if I were to call the method directly through one of the interop interfaces? That is, I am wondering if IFooBase.InvokeMember("SetValue", ...) is going to have much worse performance over IFooDouble.SetValue(11.3), especially when doing many repeated calls.





Aucun commentaire:

Enregistrer un commentaire