C# Type GetGenericParameterConstraints
Description
Type GetGenericParameterConstraints
returns an array
of Type objects that represent the constraints on the current generic type
parameter.
Syntax
Type.GetGenericParameterConstraints
has the following syntax.
public virtual Type[] GetGenericParameterConstraints()
Returns
Type.GetGenericParameterConstraints
method returns
Example
The following code example defines a generic type Test with two type parameters that have different constraints.
// ww w . j a v a 2s .co m
using System;
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)
{
Type[] tpConstraints = tp.GetGenericParameterConstraints();
foreach (Type tpc in tpConstraints)
{
Console.WriteLine("\t{0}", tpc);
}
}
}
}
The code above generates the following result.