TypeLoadException - generic in class attribute - C++/CLI Compiler
Using a generic type information as a class attribute parameter I get a
TypeLoadException at runtime (with C++/CLI). This exception occurs if the
generic parameter is defined in one assembly and used in another. Using C# no
exception is thrown - everything works perfect.
I'm using VS2008 Team System with .net Framework 3.5 SP1. The Behaviour is
the same with VS2010-beta1, .net 4.0.
Assembly/class structure for illustration:
---------
Assembly1
generic<typename T>
public ref class G
{
};
public ref class SettingsAttribute: Attribute
{
public:
SettingsAttribute(System::Type^ type):
_Type(type)
{
}
private:
System::Type^ _Type;
};
---------
Assembly2
[SettingsAttribute(G<int>::typeid)]
public ref class C
{
}
public ref class TestClass
{
public:
void TestMethod()
{
G<int>^ g = gcnew G<int>(); //works
C::typeid->GetCustomAttributes(SettingsAttribute::typeid);
//TypeLoadException!
}
}
---------
With the namespace set to "TestNS" the IL code looks like this.
The C++/CLI compiler decorates class C with wrong meta information
"[mscorlib]TestNS.G`1":
.custom instance void [Assembly1]TestNS.SettingsAttribute::.ctor(class
[mscorlib]System.Type) = { type([mscorlib]TestNS.G`1<int32>) }
The C# compiler does it correctly:
.custom instance void [Assembly1]TestNS.SettingsAttribute::.ctor(class
[mscorlib]System.Type) = { type([Assembly1]TestNS.G`1<int32>) }
---------
Is there any solution or workaround for this problem? Thank you in advance.
Martin