How to create a generic .NET type in a native C ++ application that hosts the CLR (C ++ / CLI is not used)?

In .NET programs, I can create a generic type :

 System::Type::MakeGenericType(...)

There must be a way to do this in native C ++ for the .NET type (s _TypePtr). I host my own CLR instance and do not use C ++ / CLI.

+5
source share
1 answer

Finally, I found a workaround. First I need an additional assembly that wraps System :: TypeBuilder

/// <summary>
/// Wrapper for System::TypeBuilder
/// </summary>
public class TypeBuilder
{
    /// <summary>
    /// Creates a generic type out of the given arguments.
    /// </summary>
    /// <param name="generic">The unqualified generic type.</param>
    /// <param name="typeArgs">The typearguments for the generic type.</param>
    /// <returns>The qualified generic type.</returns>
    public static Type MakeGenericType(Type generic, params Type[] typeArgs)
    {
        return generic.MakeGenericType(typeArgs);
    }
}

This is an optional add-on that I download from C ++

And there I launch these sex lines where CLRAssembly is the level of abstraction for clr.

_TypePtr CLRAssembly::BuildType(_TypePtr spGenericType, _TypePtr spTypeArgs[]) 
{
    LONG index = 0;
    SAFEARRAY* psaArgs = SafeArrayCreateVector(VT_VARIANT, 0, 1 + (sizeof(spTypeArgs)/sizeof(_TypePtr)));
    SafeArrayPutElement(psaArgs, &index, &_variant_t((IUnknown*)spGenericType, true));
    for(int i = 0; i < sizeof(spTypeArgs)/sizeof(_TypePtr); i++) {
        index++;
        SafeArrayPutElement(psaArgs, &index, &_variant_t((IUnknown*)spTypeArgs[i], true));
    }

    return (_TypePtr)clraAssemblyHelper->RunMethod(L"AssemblyHelper.TypeBuilder", L"MakeGenericType", psaArgs); 
}

, , :

DDDElements::Defs* ListDefs::AddNew()
{
    _TypePtr params[1];
    params[0] = clra->BuildType(L"DDD.Elements.Defs");
    _TypePtr spType = clra->BuildType(clra->BuildType(L"DDD.ElementList`1"), params);
    return new DDDElements::Defs(clr, clra, 
        clra->RunMethod(spType, vtCLRObject, L"AddNew")
    );
}

, .:)

/

+3

All Articles