Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between .NET's System.Type.GenericTypeArguments and System.Type.GetGenericArguments()

Why does the .NET Framework provide both

System.Type.GenericTypeArguments

and

System.Type.GetGenericArguments()

which both return the type arguments (both as a Type[]) of a given generic type ?

Seems the property and the method expose the very same functionality, meaning the API's interface has redundant/duplicate functionalities ?

like image 389
Veverke Avatar asked Feb 10 '23 09:02

Veverke


2 Answers

  • The GenericTypeArguments property returns an empty array if the type is a generic type defintion, while the GetGenericArguments method returns an array with the types for the generic arguments.
  • The GenericTypeArguments property was added in framework 4.5.

The GenericTypeArguments property is actually implemented to call GetGenericArguments when the type is an implementation of a generic type:

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

Source: http://referencesource.microsoft.com/#mscorlib/system/type.cs,0aa31a7de47b9dc7

like image 61
Guffa Avatar answered Feb 12 '23 01:02

Guffa


It will become more obvious when you look at the Version Information documentation in the MSDN articles for these members. The GenericTypeArguments property is supported for WinRT (aka Windows Store apps), the GetGenericArguments() method is not.

WinRT caused many changes in .NET Framework version 4.5, most however are not that obvious. The language projection built into the framework covers up most of the fundamental type system differences and hides the fact that WinRT is COM-based at its core. Not if you use Reflection however, it had to be tackled very differently.

like image 39
Hans Passant Avatar answered Feb 12 '23 01:02

Hans Passant