This documentation is archived and is not being maintained.

TypeBuilder::GetMethod Method (Type, MethodInfo)

Returns the method of the specified constructed generic type that corresponds to the specified method of the generic type definition.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public:
static MethodInfo^ GetMethod(
	Type^ type, 
	MethodInfo^ method
)

Parameters

type
Type: System::Type
The constructed generic type whose method is returned.
method
Type: System.Reflection::MethodInfo
A method on the generic type definition of type, which specifies which method of type to return.

Return Value

Type: System.Reflection::MethodInfo
A MethodInfo object that represents the method of type corresponding to method, which specifies a method belonging to the generic type definition of type.

ExceptionCondition
ArgumentException

method is a generic method that is not a generic method definition.

-or-

type does not represent a generic type.

-or-

type is not of type TypeBuilder.

-or-

The declaring type of method is not a generic type definition.

-or-

The declaring type of method is not the generic type definition of type.

The GetMethod method provides a way to get a MethodInfo object that represents a method of a constructed generic type whose generic type definition is represented by a TypeBuilder object.

For example, suppose you have a TypeBuilder object that represents the type G<T> in C# syntax (G(Of T) in Visual Basic, generic <T> ref class G in C++) and a MethodBuilder object that represents a method T M() in C# syntax (Function M() As T in Visual Basic, T M() in C++) that is defined by G<T>. Suppose that G<T> has a generic method with type parameter U that creates an instance of the constructed type G<U> and calls method M on that instance. In order to emit the function call, you need a MethodInfo object that represents M on the constructed type — in other words, that returns type U rather than type T. To do this, first call the MakeGenericType method on the TypeBuilder object, specifying the GenericTypeParameterBuilder object that represents U as the type argument. Then call the GetMethod method with the return value of the MakeGenericType method as parameter type and the MethodBuilder object that represents T M() as parameter method. The return value is the MethodInfo object you need to emit the function call. The code example demonstrates a scenario similar to this.

The following code example contains source code for a generic class named Sample that has a type parameter named T. The class has a field named Field, of type T, and a generic method named GM with its own type parameter, named U. Method GM creates an instance of Sample, substituting its own type parameter U for the type parameter of Sample, and stores its input parameter in Field. This source code is compiled but not used; you can view it with the Ildasm.exe (MSIL Disassembler) and compare it to the code emitted by class Example.

The code in class Example demonstrates the use of the GetMethod method to emit generic code. The Main method of class Example creates a dynamic assembly containing a class named Sample and uses the DefineGenericParameters method to make it generic by adding a type parameter named T. A default constructor and a field named Field, of type T, are added to class Sample. A method GM is added and turned into a generic method by using the MethodBuilder::DefineGenericParameters method. The type parameter of GM is named U. After the type parameter is defined, the signature of GM is added by using the MethodBuilder::SetSignature method. There is no return type and no required or custom modifiers, so all the parameters of this method are nullptr except parameterTypes; parameterTypes sets the type of the method's only parameter to U, the method's generic type parameter. The body of the method creates an instance of the constructed type Sample<U> (Sample(Of U) in Visual Basic), assigns the method's parameter to Field, and then prints the value of Field. A dummy type is defined to hold the entry-point method Main. In the body of Main, the static GM method is invoked on the constructed generic type Sample<int> (Sample(Of Integer) in Visual Basic), with type String substituted for U. The GetMethod method is used to create a MethodInfo for the static GM method of the constructed generic type Sample<U>, and the MethodInfo::MakeGenericMethod method is then used to create a MethodInfo that can emitted in a method call.

When the code example is run, it saves the emitted assembly as TypeBuilderGetFieldExample.exe. You can run TypeBuilderGetFieldExample.exe, and you can use the Ildasm.exe (MSIL Disassembler) to compare the emitted code with the code for the Sample class that is compiled into the code example itself.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: