C# TypeInfo DeclaringMethod
Description
TypeInfo DeclaringMethod
Gets a MethodBase that represents
the declaring method, if the current Type represents a type parameter of
a generic method.
Syntax
TypeInfo.DeclaringMethod
has the following syntax.
public virtual MethodBase DeclaringMethod { get; }
Example
using System;/*from ww w . j a va 2 s . c om*/
using System.Reflection;
public class Example
{
public static void Generic<T>(T toDisplay)
{
Console.WriteLine("\r\nHere it is: {0}", toDisplay);
}
}
public class Test
{
public static void Main()
{
Type ex = typeof(Example);
MethodInfo mi = ex.GetMethod("Generic");
MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));
if (mi.IsGenericMethod)
{
Type[] typeArguments = mi.GetGenericArguments();
Console.WriteLine("\tList type arguments ({0}):",
typeArguments.Length);
foreach (Type tParam in typeArguments)
{
if (tParam.IsGenericParameter)
{
Console.WriteLine("\t\t{0} parameter position {1}" +
"\n\t\t declaring method: {2}",
tParam,
tParam.GenericParameterPosition,
tParam.DeclaringMethod);
}
else
{
Console.WriteLine("\t\t{0}", tParam);
}
}
}
}
}