是否有可能将System.__ComObject转换为仅在运行时才知道的某种类型? 我有以下代码
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();
使用下面的代码可以正常工作,但不幸的是我不能在我的代码中使用InvokeMember,因为这将会非常复杂。
ComClassType.InvokeMember("ComClassMethod", BindingFlags.InvokeMethod, null, comClassInstance, null);
我想问一下,是否可以将“comClassInstance”转换为“ComClassType”,以便通过这种方式调用方法 comClassInstance.ComClassMethod();
is
,但是无法使用反射进行强制转换或使用as
。http://stackoverflow.com/q/21673363/57428。您可以尝试使用GetIUnknownForObject()和GetObjectForIUnknown()来链接,然后将结果用作`dynamic`。 - sharptooth