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.<
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));
}