Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type.GenericTypeArguments property vs Type.GetGenericArguments() method

Tags:

c#

reflection

f#

What's the difference between the Type.GenericTypeArguments property and the Type.GetGenericArguments() method? Do they always return the same thing or are there situations where they differ?

like image 841
Gustavo Guerra Avatar asked Oct 21 '13 20:10

Gustavo Guerra


2 Answers

typeof(List<>) is an example where they differ. The property returns an empty array, while the method returns an array with a generic T in it. (this T has IsGenericParameter true)

From reading the documentation, I think that you can think of GenericTypeArguments as GetGenericArguments().Where(t => !t.IsGenericParameter).ToArray(), i.e. only the concrete types. See also ContainsGenericParameters.

like image 58
Tim S. Avatar answered Oct 09 '22 07:10

Tim S.


The reference source tells the exact answer:

public virtual Type[] GenericTypeArguments{     get{         if(IsGenericType && !IsGenericTypeDefinition){             return GetGenericArguments();         }         else{             return Type.EmptyTypes;     } } 

This implementation is never overridden with something else.

like image 37
Marcel Avatar answered Oct 09 '22 07:10

Marcel