Veverke Veverke - 1 year ago 92
C# Question

Difference between IsGenericType and IsGenericTypeDefinition

What is the difference between

Type.IsGenericType
and
Type.IsGenericTypeDefinition
? Interestingly enough, MSDN's link for IsGenericTypeDefinition is broken.

After playing a bit with trying to retrieve all the DbSets defined in a given DbContext, I was lead to the following, which behavior I am trying to understand: filtering properties via IsGenericType returns the desired results, while with IsGenericTypeDefinition not (does not return any).

It's interesting that from this post I have the impression that the author did get his DbSets using IsGenericTypeDefinition, while I did not.

Follows a sample that illustrates the discussion:

private static void Main(string[] args)
{
A a = new A();
int propertyCount = a.GetType().GetProperties().Where(p => p.PropertyType.IsGenericType).Count();
int propertyCount2 = a.GetType().GetProperties().Where(p => p.PropertyType.IsGenericTypeDefinition).Count();

Console.WriteLine("count1: {0} count2: {1}", propertyCount, propertyCount2);
}

// Output: count1: 1 count2: 0

public class A
{
public string aaa { get; set; }
public List<int> myList { get; set; }
}

Answer Source

IsGenericType tells you that this instance of System.Type represents a generic type with all its type parameters specified. For example, List<int> is a generic type.

IsGenericTypeDefinition, on the other hand, tells you that this instance of System.Type represents a definition from which generic types can be constructed by supplying type arguments for its type parameters. For example, List<> is a generic type definition.

You can get a generic type definition of a generic type by calling GetGenericTypeDefinition:

var listInt = typeof(List<int>);
var typeDef = listInt.GetGenericTypeDefinition(); // gives typeof(List<>)

You can make a generic type from a generic type definition by providing it with type arguments to MakeGenericType:

var listDef = typeof(List<>);
var listStr = listDef.MakeGenericType(typeof(string));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download