Using Reflection with COM Interop

前端 未结 1 1800
日久生厌
日久生厌 2020-12-18 12:25

After an interop call, I get back a COM object. I know this object will be one of three possible COM classes (Class1, Class2, Class3), but do not know which one in runtime.<

相关标签:
1条回答
  • 2020-12-18 13:04

    In C# 4.0, dynamic would be ideal for this type of duck-typing.

    Until then, I wonder if VB.Net would be better, with Option Strict Off to allow late binding against object.

    Worst case: write it in VB.Net, then use reflector to write the C# for you ;-p

    Here's an example, that requires a reference to Microsoft.VisualBasic.dll, but is fine in C#:

    public static object GetValue(object obj, string propertyName)
    {
        return RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(obj, null,
             propertyName, new object[0], null, null, null));
    }
    
    0 讨论(0)
提交回复
热议问题