Check if a type is a generic type in CSharp

Description

The following code shows how to check if a type is a generic type.

Example


using System;/* w w w.  jav  a2  s .com*/
using System.Reflection;

public class Base<T, U> {}

public class Derived<V> : Base<string, V>
{
    public G<Derived <V>> F;

    public class Nested {}
}

public class G<T> {}

class Example
{
    public static void Main()
    {
        Type t = typeof(Derived<>);

        Console.WriteLine("    Type: {0}", t);

        Console.WriteLine("\t            IsGenericType: {0}", t.IsGenericType);
        Console.WriteLine("\t  IsGenericTypeDefinition: {0}", t.IsGenericTypeDefinition);
        Console.WriteLine("\tContainsGenericParameters: {0}", t.ContainsGenericParameters);
        Console.WriteLine("\t       IsGenericParameter: {0}", t.IsGenericParameter);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type