Dynamic listing of .__ ComObject system

Is it possible to use System .__ ComObject for some type that is known only at runtime? I have the following code

Type ComClassType = SomeDLLAssembly.GetType("ClassName");
dynamic comClassInstance = icf2.CreateInstanceLic(null, null, ComClassType.GUID, "License string");
//This will throw exception, because comClassInstance type is __ComObject and it does not contains ComClassMethod
comClassInstance.ComClassMethod();

When I use the code below, it works fine, but unfortunately I cannot use InvokeMember in my code, because it will be very difficult.

ComClassType.InvokeMember("ComClassMethod", BindingFlags.InvokeMethod, null, comClassInstance, null);

So, I would like to ask if it is possible to include "comClassInstance" in "ComClassType" in order to be able to call methods in this way comClassInstance.ComClassMethod ();

+4
source share
4 answers

which is known only at runtime?

, , . , . , . , , . __ComObject - RCW, IDispatch. , .

, . - . "------", , . # , - ​​ , Reflection. , , v4, # Visual Basic, . Reflection , DLR . , , .

, , , apis . , , , , " ". , . IntelliSense , . , , - () .

COM , . , , . .NET. , , , IntelliSense .

(.dll .exe), ,.tlb .olb . Visual Studio File > Open > File, , TYPELIB node, . OleView.exe, File > View Typelib, . Tlbimp.exe interop.

, . , .

+5

COM-.

# ( )

dynamic comClassInstance = Activator.CreateInstance(Type.GetTypeFromProgID("ClassName"));
comClassInstance.ComClassMethod();
var result = comClassInstance.ComClassFMethod(param);

vb.net( Option , ). COM vb.net #.

Object.

CreateObject COM- comClassInstance.ComClassMethod()

Public Class Abc
    Private _comClassInstance As Object

    Public Sub New()
        _comClassInstance = CreateObject("ClassName")
    End Sub

    Public Sub ComClassMethod()
        _comClassInstance.ComClassMethod()
    End Sub

    Public Function ComClassFMethod(param As String) As Integer
        Return _comClassInstance.ComClassFMethod(param)
    End Function 
End Class
+3

"ClassName" COM-, - .

Type ComClassType = SomeDLLAssembly.GetType("ClassName");
ClassName myInstance = (ClassName)icf2.CreateInstanceLic(null, null, ComClassType.GUID, "License string");
myInstance.ComClassMethod();
0

, , , , , , , icf2. , , COM, dynamic. , DynamicObject TryInvokeMember ( , , TryInvokeMember ). , , - .

, (, ..), :

public class ComWrapper : DynamicObject {
    Object _instance;
    Type _type;

    // Create + save type/instance information in constructor
    // You seem to do this differently, so you'd want to change this...
    public ComWrapper(Guid guid) {
        _type = Type.GetTypeFromCLSID(guid, true);
        _instance =  Activator.CreateInstance(_type, true);            
    }

    // Invoke requested method, passing in args and assigning return value
    // to result
    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, 
                                         out object result) {
        result = _type.InvokeMember(binder.Name, 
                                    System.Reflection.BindingFlags.InvokeMethod, null,
                          _instance, args);

        return true;  // Return true to indicate it been handled
    }
}

, , Word, , :

dynamic val = new ComWrapper(new Guid("{000209FF-0000-0000-C000-000000000046}"));

val.Quit(0, 0, false );

, , - , . . , COM-, , InvokeMethod, Does not contain a definition .

0

All Articles