How can I expose a specialized managed template class to C#?

Hi all,

I know that only referenced functions are emitted in the metadata so how can I force all of the functions in my template to be referenced. I tired explicitly creating the class like this:

template ref class MyClass<FirstType^, SecondType, ThirdType>;

That didn’t work.

I also tried this:

public ref class MyDerivedClass : public MyClass<FirstType^, SecondType, ThirdType>

{

};

That didn’t work either. I end up with MyDerivedClass and no member functions.

So, is there some way I can force this thing into metadata



Answer this question

How can I expose a specialized managed template class to C#?

  • singlenipple

    Yes, I know that only referenced functions are emitted into metadata.

    What I need is some way to force the functions in a class to be emitted into metadata even though they are not referenced in the class library. The other alternative is for me to write some 100+ versions of the same class.

    This is an interop problem or I would just use generics. The problem is that these classes are really rappers for unmanaged template classes. As such I have no way of getting the type information from a generic wrapper into the unmanaged template implementation.

    So, if I could somehow get the functions emitted my problem would be solved. I could simply put explicit instantiations of the different template classes in the class library. I could even create explicit derivations of the template class like this:

    public ref class MyTypeXDerived : public MyTemplate<TypeX> {} ;

    Doing this however I don’t get any of the base class functions in the metadata.


  • Alessandro Camargo

    I think as per the Dotnet architecture, only referenced functions are emitted into metadata. So it may not be possible to reference all the functions.
    Assume that if one function 'writeline' is called from System.Console class then all the functions in System.Console class are emitted into metadata then what's the use of this

    Feel free to reply if i could not get your question correctly.

  • David S. Anderson

    Seeing as no one seems to have any suggestions I've had to come up with some solution and I'm sure it’s not the best. In fact, it’s pretty crappy.

    Anyway, I made the class on huge macro that I pass the type to as a macro parameter. Using this method I can create several specialized versions of the managed wrapper for the unmanaged data.

    This is the basic idea and accomplishes what I need (I have no idea if this example will compile).

    #define MY_CLAS(_managed_, _managed_item_, _native_) \

    public ref class _managed_##Container \

    { \

    private: \

    _native_ * m_nativeData; \

    \

    internal: \

    _managed_##Container(_native_ * data) \

    { \

    m_nativeData = data; \

    } \

    public: \

    _managed_item_ ^ GetItem(int i) \

    { \

    return gcnew _managed_item_( m_nativeData->GetItem(i)); \

    } \

    }

    If anyone has a better way to do something like this please speak up!


  • How can I expose a specialized managed template class to C#?