C# TypeInfo GetGenericParameterConstraints
Description
TypeInfo GetGenericParameterConstraints
Returns
an array of Type objects that represent the constraints on the current generic
type parameter.
Syntax
TypeInfo.GetGenericParameterConstraints
has the following syntax.
public virtual Type[] GetGenericParameterConstraints()
Returns
TypeInfo.GetGenericParameterConstraints
method returns
Example
using System;/*from w ww . j av a 2 s. co m*/
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<,>);
Type[] defparams = def.GetGenericArguments();
foreach (Type tp in defparams)
{
Console.WriteLine("\r\nType parameter: {0}", tp.Name);
Type[] tpConstraints = tp.GetGenericParameterConstraints();
foreach (Type tpc in tpConstraints)
{
Console.WriteLine("\t{0}", tpc);
}
}
}
}
The code above generates the following result.