This documentation is archived and is not being maintained.

MethodBuilder.MakeGenericMethod Method

Returns a generic method constructed from the current generic method definition using the specified generic type arguments.

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

Public Overrides Function MakeGenericMethod ( _
	ParamArray typeArguments As Type() _
) As MethodInfo


Type: System.Type()
An array of Type objects that represent the type arguments for the generic method.

Return Value

Type: System.Reflection.MethodInfo
A MethodInfo representing the generic method constructed from the current generic method definition using the specified generic type arguments.

When you are emitting dynamic code, you might need to emit a call to a method constructed from the generic method definition represented by a MethodBuilder, before the enclosing type has been completed. You can use the MakeGenericMethod method to create a MethodInfo for such a constructed method, and use the MethodInfo in the emitted call.

The following code example creates a constructed method from an incomplete generic method definition in an incomplete type.

The example creates a transient assembly and module with a single type, adds a method M, and makes the method generic by adding a type parameter T using the DefineGenericParameters method. The type parameter is used as the type of the method's parameter, and also as its return type. The generic method definition is not given a body, and the enclosing type is not completed. The MakeGenericMethod method is then used to make the constructed method M<String> (M(Of String) in Visual Basic). The example code has no output, because the subclass of MethodInfo returned by the MakeGenericMethod method does not allow reflection over its parameters.


For another code example that uses MakeGenericMethod, see DefineGenericParameters. MakeGenericMethod is also used extensively when emitting code that uses generic types. See How to: Define a Generic Method with Reflection Emit.

Imports System
Imports System.Reflection
Imports System.Reflection.Emit

Class Example

    Public Shared Sub Main()

        ' Define a transient dynamic assembly (only to run, not
        ' to save) with one module and a type "Test".
        Dim aName As AssemblyName = New AssemblyName("MyDynamic")
        Dim ab As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                aName, _
        Dim mb As ModuleBuilder = ab.DefineDynamicModule(aName.Name)
        Dim tb As TypeBuilder = mb.DefineType("Test")

        ' Add a Public Shared method "M" to Test, and make it a
        ' generic method with one type parameter named "T").
        Dim meb As MethodBuilder = tb.DefineMethod("M", _
            MethodAttributes.Public Or MethodAttributes.Static)
        Dim typeParams() As GenericTypeParameterBuilder = _
            meb.DefineGenericParameters(New String() { "T" })

        ' Give the method one parameter, of type T, and a 
        ' return type of T.

        ' Create a MethodInfo for M(Of String), which can be used 
        ' in emitted code. This is possible even though the method
        ' does not yet have a body, and the enclosing type is not
        ' created.
        Dim mi As MethodInfo = _
        ' Note that this is actually a subclass of MethodInfo, 
        ' which has rather limited capabilities -- for
        ' example, you cannot reflect on its parameters.
    End Sub
End Class

.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.