C# TypeInfo GetGenericArguments
Description
TypeInfo GetGenericArguments
Returns an array of Type
objects that represent the type arguments of a generic type or the type parameters
of a generic type definition.
Syntax
TypeInfo.GetGenericArguments
has the following syntax.
public virtual Type[] GetGenericArguments()
Returns
TypeInfo.GetGenericArguments
method returns
Example
using System;/*from ww w .j a v a2 s . c om*/
using System.Reflection;
public interface ITest {}
public class Base {}
public class Test<T,U>
where T : Base, ITest
where U : class, new() {}
public class Derived : Base, ITest {}
public class Example
{
public static void Main()
{
Type def = typeof(Test<,>);
Console.WriteLine("\r\nExamining generic type {0}", def);
Type[] defparams = def.GetGenericArguments();
foreach (Type tp in defparams)
{
Console.WriteLine("\r\nType parameter: {0}", tp.Name);
Console.WriteLine("\t{0}", ListGenericParameterAttributes(tp));
}
}
private static string ListGenericParameterAttributes(Type t)
{
string retval;
GenericParameterAttributes gpa = t.GenericParameterAttributes;
GenericParameterAttributes variance = gpa & GenericParameterAttributes.VarianceMask;
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);
}
}
}
return "";
}
}
The code above generates the following result.