Question

I have a class:

public class A
{
    public void Test()
    {
        Type t = icom.ReturnType;
    }
}

[ComVisible(true),
Guid("81C99F84-AA95-41A5-868E-62FAC8FAC263"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface Icom
{
    Type ReturnType { get; }
}

[ComVisible(true)]
[Guid("6DF6B926-8EB1-4333-827F-DD814B868097")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(Icom))]
public class B : Icom
{
    public Type ReturnType
    {
        get
        {
            return SomeAssembly.GetType("fullname");
        }
    }
}

Is this somehow possible to accomplish in .NET 3.5? My goal is to return System.Type from class B. In this case I receive an error that Unable to cast System.__Com to System.Type.

Was it helpful?

Solution

I'll try to explain what's going on, as I understand it. It appears you have the following workflow of calls (or objects being passing over): .NET 4.x -> COM -> .NET 3.5. Your Icom interface is marshaled to COM like this (OleView):

[
  odl,
  uuid(81C99F84-AA95-41A5-868E-62FAC8FAC263),
  version(1.0),
  dual,
  oleautomation,
  custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "InteropTest.Icom")    

]
interface Icom : IDispatch {
    [id(0x60020000), propget]
    HRESULT ReturnType([out, retval] _Type** pRetVal);
};

Note the _Type which is unmanaged proxy for managed Type class, implemented in mscorlib.dll (type library mscorlib.tlb). Its implementation is specific to each .NET Runtime version. Apparently, it gets created by .NET Runtime v4.0.

At some point later, you're trying to un-marshal it back, this time on .NET 3.5 side. If it was v4.0, you'd get back the same managed Type object. However, with v3.5 you are getting an unamaged System.__ComObject, which cannot be converted to v3.5 managed Type object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top