Interfaces:

Interfaces are emulated with wrappers.

For example, here is a simple C# Code:

interface IMyInterface
{
    int Function(int a);
}

class MyClass : IMyInterface
{
    public int Function(int a)
    {
        return (a);
    }
}


The corresponding generated C++ code is:

In the header file:
class InterfaceTest__IMyInterface : public ::CrossNetRuntime::IInterface
{
    CN_DYNAMIC_ID()
    ...
    virtual ::System::Int32 Function(void * __passed_instance__, ::System::Int32 a) = 0;
    ...
};
class InterfaceTest__MyClass : public ::System::Object
{
    CN_DYNAMIC_ID()
    ...
    ::System::Int32 Function(::System::Int32 a);
    ...
    class Wrapper__InterfaceTest__IMyInterface : public ::CSharpBenchmark::_Benchmark::InterfaceTest__IMyInterface
    {
    public:
        virtual ::System::Int32 Function(void * __passed_instance__, ::System::Int32 a);
        ...
    };
    ...
};

In the source file:
::System::Int32 CSharpBenchmark::_Benchmark::InterfaceTest__MyClass::Function(::System::Int32 a)
{
    return (a);
}

::System::Int32 CSharpBenchmark::_Benchmark::InterfaceTest__MyClass::Wrapper__InterfaceTest__IMyInterface::Function(void * __passed_instance__, ::System::Int32 a)
{
    ::CSharpBenchmark::_Benchmark::InterfaceTest__MyClass * __instance__ = static_cast<::CSharpBenchmark::_Benchmark::InterfaceTest__MyClass *>(__passed_instance__);
    return (__instance__->Function( a));
}



The calling convention for interface is slightly different:

C# version:

result = myInterface0.Function(42);
if (result != 42)
{
    return (false);
}


Generated C++ version:

::CSharpBenchmark::_Benchmark::InterfaceTest__IMyInterface * __temp299__ = interface2;
if ((INTERFACE__CALL(__temp299__, ::CSharpBenchmark::_Benchmark::InterfaceTest__IMyInterface)->Function(__temp299__, 42) != 42))
{
    return (false);
}

Last edited Sep 7, 2007 at 7:41 PM by OlivierNallet, version 1

Comments

No comments yet.