System.Reflection.MethodInfo Class

public abstract class MethodInfo : MethodBase

Base Types

Object
  MemberInfo
    MethodBase
      MethodInfo

Assembly

mscorlib

Library

Reflection

Summary

Discovers the attributes of a method and provides access to method metadata.

Description

Instances of MethodInfo are obtained by calling the System.Type.GetMethods or System.Type.GetMethod method of a Type object or of an object that derives from System.Type, or by calling the System.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) method of a MethodInfo that represents a generic method definition.

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

[Note: When operating on the given kinds of methods, the following properties return the result as shown:

PropertyNon-Generic
IsGenericMethodDefinition False
ContainsGenericParameters False
IsGenericMethod False
]

See Also

System.Reflection Namespace

Members

MethodInfo Constructors

MethodInfo Constructor

MethodInfo Methods

MethodInfo.GetBaseDefinition Method
MethodInfo.GetGenericArguments Method
MethodInfo.GetGenericMethodDefinition Method
MethodInfo.MakeGenericMethod Method

MethodInfo Properties

MethodInfo.ContainsGenericParameters Property
MethodInfo.IsGenericMethod Property
MethodInfo.IsGenericMethodDefinition Property
MethodInfo.ReturnType Property


MethodInfo Constructor

protected MethodInfo();

Summary

Constructs a new instance of the MethodInfo class.

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace

MethodInfo.GetBaseDefinition Method

public abstract MethodInfo GetBaseDefinition();

Summary

Returns a new MethodInfo instance that reflects the first definition of the method reflected by the current instance in the inheritance hierarchy of that method.

Return Value

A new MethodInfo instance that reflects the first definition of the method reflected by the current instance in the inheritance hierarchy of that method.

Description

[Behaviors: System.Reflection.MethodInfo.GetBaseDefinition proceeds along the inheritance hierarchy of the method reflected by the current instance, returning a MethodInfo instance that reflects the first definition in the hierarchy of that method.

The method declaration to be reflected by the new MethodInfo instance is determined as follows:

]

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace

MethodInfo.GetGenericArguments Method

public override Type[] GetGenericArguments()

Summary

Returns an array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition.

Return Value

An array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition. Returns an empty array if the current method is not a generic method.

Description

The elements of the returned array are in the order in which they appear in the list of type parameters for the generic method.

If the current method is a closed constructed method (that is, the System.Reflection.MethodInfo.ContainsGenericParameters property returns false ), the array returned by the System.Reflection.MethodInfo.GetGenericArguments method contains the types that have been assigned to the generic type parameters of the generic method definition.

If the current method is a generic method definition, the array contains the type parameters.

If the current method is an open constructed method (that is, the System.Reflection.MethodInfo.ContainsGenericParameters property returns true ) in which specific types have been assigned to some type parameters and type parameters of enclosing generic types have been assigned to other type parameters, the array contains both types and type parameters. Use the System.Type.IsGenericParameter property to tell them apart.

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

Example

The following code shows how to get the type arguments of a generic method and display them. (It is part of a larger example for the method System.Reflection.MethodInfo.MakeGenericMethod.)

// If this is a generic method, display its type arguments.
//
if (mi.IsGenericMethod)
{
        Type[] typeArguments = mi.GetGenericArguments();

        Console.WriteLine("\tList type arguments ({0}):", 
                typeArguments.Length);

        foreach (Type tParam in typeArguments)
        {
                // IsGenericParameter is true only for generic type
                // parameters.
                //
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}  (unbound - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
        else
        {
            Console.WriteLine("\tThis is not a generic method.");
        }
}

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace

MethodInfo.GetGenericMethodDefinition Method

public override MethodInfo GetGenericMethodDefinition()

Summary

Returns a MethodInfo object that represents a generic method definition from which the current method can be constructed.

Return Value

A MethodInfo object representing a generic method definition from which the current method can be constructed.

Exceptions

Exception TypeCondition
InvalidOperationExceptionThe current method is not a generic method. That is, System.Reflection.MethodInfo.IsGenericMethod returns false .

Description

If you call System.Reflection.MethodInfo.GetGenericMethodDefinition on a MethodInfo that already represents a generic method definition, it returns the current MethodInfo.

If a generic method definition includes generic parameters of the declaring type, there will be a generic method definition specific to each constructed type.

A generic method definition is a template from which methods can be constructed. For example, from the generic method definition T M<T>(T t) you can construct and invoke the method int M<int>(int t) . Given a MethodInfo object representing this constructed method, the System.Reflection.MethodInfo.GetGenericMethodDefinition method returns the generic method definition.

If two constructed methods are created from the same generic method definition, the System.Reflection.MethodInfo.GetGenericMethodDefinition method returns the same MethodInfo object for both methods.

If you call System.Reflection.MethodInfo.GetGenericMethodDefinition on a MethodInfo that already represents a generic method definition, it returns the current MethodInfo.

If a generic method definition includes generic parameters of the declaring type, there will be a generic method definition specific to each constructed type. For example, consider the following C# code:

class B<U,V> {}

class C<T> { B<T,S> M<S>() {}}

In the constructed type C<int> , the generic method M returns B<int, S> . In the open type C<T> , M returns B<T, S> . In both cases, the System.Reflection.MethodInfo.IsGenericMethodDefinition property returns true for the MethodInfo that represents M , soSystem.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) can be called on both MethodInfo objects. In the case of the constructed type, the result of calling System.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) is a MethodInfo that can be invoked. In the case of the open type, the MethodInfo returned by System.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) cannot be invoked.

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

Example

The following code shows a class with a generic method and the code required to obtain a MethodInfo for the method, bind the method to type arguments, and get the original generic type definition back from the bound method. (It is part of a larger example for the method System.Reflection.MethodInfo.MakeGenericMethod.)

// Define a class with a generic method.
public class Example
{
    public static void Generic<T>(T toDisplay)
    {
        Console.WriteLine("\nHere it is: {0}", toDisplay);
    }
}

// ...
// Create a Type object representing class Example, and
// get a MethodInfo representing the generic method.
//
Type ex = Type.GetType("Example");
MethodInfo mi = ex.GetMethod("Generic");

DisplayGenericMethodInfo(mi);

// Bind the type parameter of the Example method to 
// type int.
//
Type[] arguments = {typeof(int)};
MethodInfo miBound = mi.MakeGenericMethod(arguments);

DisplayGenericMethodInfo(miBound);


// ...
// Get the generic type definition from the closed method,
// and show it's the same as the original definition.
//
MethodInfo miDef = miBound.GetGenericMethodDefinition();
Console.WriteLine("\nThe definition is the same: {0}",
       miDef == mi);

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace

MethodInfo.MakeGenericMethod Method

public override MethodInfo MakeGenericMethod(params System.Type[] typeArguments)

Summary

Substitutes the elements of an array of types for the type parameters of the current generic method definition, and returns a MethodInfo object representing the resulting constructed method.

Parameters

typeArguments
An array of types to be substituted for the type parameters of the current generic method.

Return Value

A MethodInfo object that represents the constructed method formed by substituting the elements of typeArguments for the type parameters of the current generic method definition.

Exceptions

Exception TypeCondition
ArgumentExceptionThe number of elements in typeArguments is not the same as the number of type parameters of the current generic method definition.

-or-

An element of typeArguments does not satisfy the constraints specified for the corresponding type parameter of the current generic method definition.

ArgumentNullExceptiontypeArguments is null .

-or-

Any element of typeArguments is null .

InvalidOperationExceptionThe current MethodInfo does not represent the definition of a generic method. (That is, System.Reflection.MethodInfo.IsGenericMethodDefinition returns false ).

Description

The System.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) method allows you to write code that assigns specific types to the type parameters of a generic method definition, thus creating a MethodInfo object that represents a particular constructed method. If the System.Reflection.MethodInfo.ContainsGenericParameters property of this MethodInfo object returns true , you can use it to invoke the method or to create a delegate to invoke the method.

Methods constructed with the System.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) method can be open; that is, some of their type arguments can be type parameters of enclosing generic types. You might use such open constructed methods when you generate dynamic assemblies. For example, consider the following C# code:

class C
{
  T N<T,U>(T t, U u) {...}
  public V M<V>(V v)
  {
    return N<V,int>(v, 42);
  }
}

The method body of MM contains a call to method N , specifying the type parameter of M and the type Int32. The System.Reflection.MethodInfo.IsGenericMethodDefinition property returns false for method N<V,int> . The System.Reflection.MethodInfo.ContainsGenericParameters property returns true , so method N<V,int> cannot be invoked.

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

Example

The following code demonstrates the properties and methods of System.Reflection.MethodInfo that support the examination of generic methods. The example does the following:

  1. Defines a class that has a generic method.
  2. Creates a MethodInfo that represents the generic method.
  3. Displays properties of the generic method definition.
  4. Binds the MethodInfo to a type, and invokes it.
  5. Displays properties of the bound generic method.
  6. Retrieves the generic method definition from the bound method.
using System;
using System.Reflection;

// Define a class with a generic method.
public class Example
{
    public static void Generic<T>(T toDisplay)
    {
        Console.WriteLine("\nHere it is: {0}", toDisplay);
    }
}

public class Test
{
    public static void Main()
    {
        Console.WriteLine("\n--- Examine a generic method.");

        // Create a Type object representing class Example, and
        // get a MethodInfo representing the generic method.
        //
        Type ex = Type.GetType("Example");
        MethodInfo mi = ex.GetMethod("Generic");

        DisplayGenericMethodInfo(mi);

        // Bind the type parameter of the Example method to 
        // type int.
        //
        Type[] arguments = {typeof(int)};
        MethodInfo miBound = mi.MakeGenericMethod(arguments);

        DisplayGenericMethodInfo(miBound);

        // Invoke the method.
        object[] args = {42};
        miBound.Invoke(null, args);

        // Invoke the method normally.
        Example.Generic<int>(42);

        // Get the generic type definition from the closed method,
        // and show it's the same as the original definition.
        //
        MethodInfo miDef = miBound.GetGenericMethodDefinition();
        Console.WriteLine("\nThe definition is the same: {0}",
            miDef == mi);
    }
        
    private static void DisplayGenericMethodInfo(MethodInfo mi)
    {
        Console.WriteLine("\n{0}", mi);

        Console.WriteLine("\tIs this a generic method definition? {0}", 
            mi.IsGenericMethodDefinition);

        Console.WriteLine("\tDoes it have generic arguments? {0}", 
            mi.IsGenericMethod);

        Console.WriteLine("\tDoes it have unbound generic parameters? {0}", 
            mi.ContainsGenericParameters);

        // If this is a generic method, display its type arguments.
        //
        if (mi.IsGenericMethod)
        {
            Type[] typeArguments = mi.GetGenericArguments();

            Console.WriteLine("\tList type arguments ({0}):", 
                typeArguments.Length);

            foreach (Type tParam in typeArguments)
            {
                // IsGenericParameter is true only for generic type
                // parameters.
                //
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}  (unbound - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
        else
        {
            Console.WriteLine("\tThis is not a generic method.");
        }
    }
}

/* This example produces the following output:

--- Examine a generic method.

Void Generic[T](T)
        Is this a generic method definition? True
        Does it have generic arguments? True
        Does it have unbound generic parameters? True
        List type arguments (1):
                T  (unbound - parameter position 0)

Void Generic[Int32](Int32)
        Is this a generic method definition? False
        Does it have generic arguments? True
        Does it have unbound generic parameters? False
        List type arguments (1):
                System.Int32

Here it is: 42

Here it is: 42

The definition is the same: True

 */

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace

MethodInfo.ContainsGenericParameters Property

public override bool ContainsGenericParameters { get; }

Summary

Gets a value that indicates whether a generic method contains unassigned generic type parameters.

Property Value

true if the MethodInfo contains unassigned generic type parameters; otherwise false .

Description

In order to invoke a generic method, there must be no generic type definitions or open constructed types in the type arguments of the method itself, or in any enclosing types. If the System.Reflection.MethodInfo.ContainsGenericParameters property returns true , the method cannot be invoked.

The System.Reflection.MethodInfo.ContainsGenericParameters property searches recursively for type parameters. For example, it returns true for any method in an open type A<T> , even though the method itself is not generic. Contrast this with the behavior of the System.Reflection.MethodInfo.IsGenericMethod property, which returns false for such a method.

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

[Behaviors: This property is read-only.

]

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace

MethodInfo.IsGenericMethod Property

public override bool IsGenericMethod { get; }

Summary

Returns a value that indicates whether the current method is a generic method.

Property Value

true if the current method is a generic method; otherwise false .

Description

Use the System.Reflection.MethodInfo.IsGenericMethod property to determine whether a System.Reflection.MethodInfo object represents a generic method. Use the System.Reflection.MethodInfo.ContainsGenericParameters property to determine whether a MethodInfo object represents an open constructed method or a closed constructed method.

The following table summarizes the invariant conditions for terms specific to generic methods. For other terms used in generic reflection, such as generic type parameter and generic type, see the System.Type.IsGenericType property.

TermInvariant
generic method definitionThe System.Reflection.MethodInfo.IsGenericMethodDefinition property is true .

Defines a generic method. A constructed method is created by calling the System.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) method on a MethodInfo object that represents a generic method definition, and specifying an array of type arguments.

System.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) can be called only on generic method definitions.

Any generic method definition is a generic method, but the converse is not true.

generic methodThe System.Reflection.MethodInfo.IsGenericMethod property is true .

Can be a generic method definition, an open constructed method, or a closed constructed method.

open constructed methodThe System.Reflection.MethodInfo.ContainsGenericParameters property is true .

It is not possible to invoke an open constructed method.

closed constructed methodThe System.Reflection.MethodInfo.ContainsGenericParameters property is false .

When examined recursively, the method has no unassigned generic parameters. The containing type has no generic type parameters, and none of the type arguments have generic type parameters.

[Behaviors: This property is read-only.

]

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace

MethodInfo.IsGenericMethodDefinition Property

public override bool IsGenericMethodDefinition { get; }

Summary

Gets a value that indicates whether the current System.Reflection.MethodInfo represents the definition of a generic method.

Property Value

true if the MethodInfo object represents the definition of a generic method; otherwise false .

Description

If the current System.Reflection.MethodInfo represents a generic method definition, then:

Use the System.Reflection.MethodInfo.IsGenericMethodDefinition property to determine whether type arguments have been assigned to the type parameters of a generic method. If type arguments have been assigned, the System.Reflection.MethodInfo.IsGenericMethodDefinition property returns false even if some of the type arguments are Type objects that represent type parameters of enclosing types. For example, consider the following C# code:

class C
{
  T N<T,U>(T t, U u) {...}
  public V M<V>(V v)
  {
    return N<V,int>(v, 42);
  }
}
The method body of M contains a call to method N , specifying the type parameter of M and the type Int32. The System.Reflection.MethodInfo.IsGenericMethodDefinition property returns false for method N<V,int> .

[Note: Although the open constructed method N<V,int> is not encountered when reflecting over class C , it must be generated using System.Reflection.MethodInfo.MakeGenericMethod(System.Type[])(System.Type[]) .

]

If a generic method definition includes generic parameters of the declaring type, there will be a generic method definition specific to each constructed type. For example, consider the following C# code:

class B<U,V> {}
class C<T> { B<T,S> M<S>() {}}
In the constructed type C<int> , the generic method M returns B<int, S> . In the open type C<T> , M returns B<T, S> . In both cases, the System.Reflection.MethodInfo.IsGenericMethodDefinition property returns true for the MethodInfo that represents M .

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

[Note: See the MethodInfo type for an example of the use of this property.

]

[Behaviors: This property is read-only.

]

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace

MethodInfo.ReturnType Property

public abstract Type ReturnType { get; }

Summary

Gets the type of the return value of the method reflected by the current instance.

Property Value

The Type of the return value of the method reflected by the current instance. This property is equal to the Type object representing Void if the return value of the method is void .

Description

[Behaviors: This property is read-only.]

See Also

System.Reflection.MethodInfo Class, System.Reflection Namespace