C# Type IsGenericParameter

Description

Type IsGenericParameter gets a value indicating whether the current Type represents a type parameter in the definition of a generic type or method.

Syntax

Type.IsGenericParameter has the following syntax.


public virtual bool IsGenericParameter { get; }

Example

The following example uses the IsGenericParameter property to test for generic type parameters in a generic type.


//  w  w w .  j av a 2  s.  c  o m
using System;
using System.Reflection;
using System.Collections.Generic;

public class Base<T, U> { }

public class Derived<V> : Base<int, V> { }

public class Test
{
    public static void Main()
    {
        Type derivedType = typeof(Derived<>);
        DisplayGenericTypeInfo(derivedType);

        // Display its open constructed base type.
        DisplayGenericTypeInfo(derivedType.BaseType);
    }

    private static void DisplayGenericTypeInfo(Type t)
    {
        
        if (t.IsGenericType)
        {
            Type[] typeArguments = t.GetGenericArguments();
        
            foreach (Type tParam in typeArguments)
            {
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version