When does the following line of code could fail with "The number of generic arguments provided doesn't equal the arity of the generic
type definition"?
Where SomeGenericClass is defined as:
Answer (thx for Elena):
If SomeGenericClass is defined inside another generic type, its generic types arguments will consist of root type generic arguments and its own generic arguments. MakeGenricType will cut of all generic type arguments, so you have to provide types for base type definition too:
var t = typeof (SomeGenericClass<,,>).MakeGenericType (typeof (int), typeof (int), typeof (int));
public class SomeGenericClass<T, T1, T2> {
}
If SomeGenericClass is defined inside another generic type, its generic types arguments will consist of root type generic arguments and its own generic arguments. MakeGenricType will cut of all generic type arguments, so you have to provide types for base type definition too:
public class SomeRootClass<T3> { public class SomeGenericClass<T, T1, T2> { } ...
var t = typeof (SomeGenericClass<,,>).MakeGenericType (typeof (T3), typeof (int), typeof (int), typeof (int));
...
}
public class SomeGenericClass1<U>
ReplyDelete{
public class SomeGenericClass<T, T1, T2>
{
}
public void MakeSome()
{
var t = typeof (SomeGenericClass&<,,>).MakeGenericType (typeof (int), typeof (int), typeof (int));
}
}
void Main()
{
var u = new SomeGenericClass1<string>();
u.MakeSome();
}
Yes, you are right.
ReplyDelete