c# - Get number of generic type parameters -


is there way number of parameters in unbound generic type? example:

f(typeof(list<>))        => 1 f(typeof(dictionary<,>)) => 2 

upd

i know getting number type.name maybe there way type directly

you have generic type definition. need generic type arguments:

type.getgenericarguments().length 

edit:

as sebastian noted, may give results might surprising if you're used dealing c#, because c# hides generic type arguments "inherited". example:

void main() {   typeof(a<>.nested<>).getgenericarguments().dump(); }  public class a<t> {   public class nested<v>   {    } } 

will give { typeof(t), typeof(v) }. how .net class - that's how need refer it, how you'd create through reflection etc.

this matters if you're trying c# compiler in regards classes not known @ compile-time; means you're trying generate c# code. there doesn't seem supported way of getting behaviour c# has safely - you'll have use real code generator instead of slapping strings :)


Comments