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.