There is a 3rd-party .Net assembly that defines an imported COM interface, gets an object from another COM object and casts it to the imported interface:
[ComImport, Guid(...), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IMyInterface
{
//...
}
void SomeMethod(object obj)
{
IMyInterface iface = obj as IMyInterface ;
if (iface == null)
throw("Cannot get IMyInterface");
}
The method is public, while the imported COM interface is internal. How do I create my own managed object that implements that COM interface? The obvious solution of re-importing the same interface in my assembly does not work:
[ComImport, Guid(...), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IMyInterface
{
//...
}
class MyClass : IMyInterface
{
}
SomeMethod(new MyClass());
Despite having the same GUID and being marked as a COM interface, the .Net runtime treats the imported interfaces as 2 different interfaces and does not cast my object to the interface declared in the other assembly.
I cannot reference the imported interface from the first assembly because it's not declared as public. Can I somehow instruct the .Net runtime to create an RCW for my managed object and hide the actual one or otherwise override the default casting behavior?
I am aware of the dynamic binder, however the code needs to run on .Net 2.0+, so it is unfortunately not an option.