How to create a generic .NET type in a native c++ application hosting the CLR (not using C++/CLI)?
In .NET programs I can create a generic type by:
System::Type::MakeGenericType(...)
There must be a way to do that in native C++ for a .NET type (with _TypePtr
). I am hosting my own CLR instance and not using C++/CLI. (Here's an example of how that can be done.) My approach for that is:
_TypePtr BuildGenericType(_TypePtr spGenericType, _TypePtr spTypeArgs[])
{
return spGenericType-> ..... ???
}
But there is no method like MakeGenericType开发者_运维百科
, and I don't know where to find it. Any ideas on how to solve this?
Finally i found a workaround myself. At 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 additional assemly i load from c++
And there i run these sexy lines, where CLRAssembly is an abstraction layer 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);
}
Finally i can run my example method like this:
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")
);
}
Finally it works pretty good. :)
/Solved
精彩评论