This documentation is archived and is not being maintained.

MethodBuilder.SetSignature Method

Sets the method signature, including the return type, the parameter types, and the required and optional custom modifiers of the return type and parameter types.

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

Public Sub SetSignature ( _
	returnType As Type, _
	returnTypeRequiredCustomModifiers As Type(), _
	returnTypeOptionalCustomModifiers As Type(), _
	parameterTypes As Type(), _
	parameterTypeRequiredCustomModifiers As Type()(), _
	parameterTypeOptionalCustomModifiers As Type()() _
Dim instance As MethodBuilder 
Dim returnType As Type 
Dim returnTypeRequiredCustomModifiers As Type()
Dim returnTypeOptionalCustomModifiers As Type()
Dim parameterTypes As Type()
Dim parameterTypeRequiredCustomModifiers As Type()()
Dim parameterTypeOptionalCustomModifiers As Type()()

instance.SetSignature(returnType, returnTypeRequiredCustomModifiers, _
	returnTypeOptionalCustomModifiers, _
	parameterTypes, parameterTypeRequiredCustomModifiers, _


Type: System.Type

The return type of the method.

Type: System.Type()

An array of types representing the required custom modifiers, such as IsConst, for the return type of the method. If the return type has no required custom modifiers, specify Nothing.

Type: System.Type()

An array of types representing the optional custom modifiers, such as IsConst, for the return type of the method. If the return type has no optional custom modifiers, specify Nothing.

Type: System.Type()

The types of the parameters of the method.

Type: System.Type()()

An array of arrays of types. Each array of types represents the required custom modifiers for the corresponding parameter, such as IsConst. If a particular parameter has no required custom modifiers, specify Nothing instead of an array of types. If none of the parameters have required custom modifiers, specify Nothing instead of an array of arrays.

Type: System.Type()()

An array of arrays of types. Each array of types represents the optional custom modifiers for the corresponding parameter, such as IsConst. If a particular parameter has no optional custom modifiers, specify Nothing instead of an array of types. If none of the parameters have optional custom modifiers, specify Nothing instead of an array of arrays.


The current method is generic, but is not a generic method definition. That is, the IsGenericMethod property is true, but the IsGenericMethodDefinition property is false.

If the return type and the number and types of the parameters are known when the method is defined, they can be established using any overload of the TypeBuilder.DefineMethod method that accepts an array of parameter types. However, a generic method can have parameters whose types are specified by one or more of its own generic type parameters, which cannot be defined until after the method has been defined. Use this method to set the parameter types in that case.

If neither the return type nor the parameter types have optional or required custom modifiers, such as IsConstModifier, you can use the SetReturnType and SetParameters methods.

Calling this method replaces the parameters and return type established using the TypeBuilder.DefineMethod method.

The following code example contains source code for a generic class named Sample that has a type parameter T. The class has a field named Field, of type T, and a generic method GM with its own type parameter, 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 MSIL Disassembler (Ildasm.exe), and compare it to the code emitted by class Example.

The code in class Example demonstrates the use of the SetSignature method in emitting generic code. The Main method of class Example creates a dynamic assembly containing a class named Sample, and uses the TypeBuilder.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 using the DefineGenericParameters method. The type parameter of GM is named U. Once the type parameter is defined, the signature of GM is added, using the SetSignature method. There is no return type, and no required or custom modifiers, so all the parameters of this method are Nothing except parameterTypes, which sets the type of the only parameter of the method; this is set to the method's type parameter, U. 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 TypeBuilder.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 MSIL Disassembler (Ildasm.exe) to compare the emitted code with the code for the Sample class that is compiled into the code example itself.

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

' Compare the MSIL in this class to the MSIL 
' generated by the Reflection.Emit code in class 
' Example. 
Public Class Sample(Of T)
    Public Field As T
    Public Shared Sub GM(Of U)(ByVal val As U)
        Dim s As New Sample(Of U)
        s.Field = val

    End Sub 
End Class  

Public Class Example

    Public Shared Sub Main() 
        Dim myDomain As AppDomain = AppDomain.CurrentDomain
        Dim myAsmName As New AssemblyName("TypeBuilderGetFieldExample")
        Dim myAssembly As AssemblyBuilder = _
            myDomain.DefineDynamicAssembly(myAsmName, _
        Dim myModule As ModuleBuilder = _
            myAssembly.DefineDynamicModule(myAsmName.Name, _
                myAsmName.Name & ".exe")

        ' Define the sample type. 
        Dim myType As TypeBuilder = myModule.DefineType( _
            "Sample", _
            TypeAttributes.Class Or TypeAttributes.Public)

        ' Add a type parameter, making the type generic. 
        Dim typeParamNames() As String = { "T" }
        Dim typeParams As GenericTypeParameterBuilder() = _

        ' Define a default constructor. Normally it would  
        ' not be necessary to define the default constructor, 
        ' but in this case it is needed for the call to 
        ' TypeBuilder.GetConstructor, which gets the default 
        ' constructor for the generic type constructed from  
        ' Sample(Of T), in the generic method GM(Of U). 
        Dim ctor As ConstructorBuilder = _
            myType.DefineDefaultConstructor( _
                MethodAttributes.PrivateScope Or MethodAttributes.Public _
                Or MethodAttributes.HideBySig Or MethodAttributes.SpecialName _
                Or MethodAttributes.RTSpecialName)

        ' Add a field of type T, with the name Field. 
        Dim myField As FieldBuilder = myType.DefineField( _
            "Field", typeParams(0), FieldAttributes.Public)

        ' Add a method and make it generic, with a type  
        ' parameter named U. Note how similar this is to  
        ' the way Sample is turned into a generic type. The 
        ' method has no signature, because the type of its 
        ' only parameter is U, which is not yet defined. 
        Dim genMethod As MethodBuilder = _
            myType.DefineMethod("GM", _
                MethodAttributes.Public Or MethodAttributes.Static)
        Dim methodParamNames() As String = { "U" }
        Dim methodParams As GenericTypeParameterBuilder() = _

        ' Now add a signature for genMethod, specifying U 
        ' as the type of the parameter. There is no return value 
        ' and no custom modifiers.
        genMethod.SetSignature(Nothing, Nothing, Nothing, _
            New Type() { methodParams(0) }, Nothing, Nothing)

        ' Emit a method body for the generic method. 
        Dim ilg As ILGenerator = genMethod.GetILGenerator()
        ' Construct the type Sample(Of U) using MakeGenericType. 
        Dim SampleOfU As Type = _
        ' Create a local variable to store the instance of 
        ' Sample(Of U).
        ' Call the default constructor. Note that it is  
        ' necessary to have the default constructor for the 
        ' constructed generic type Sample(Of U); use the  
        ' TypeBuilder.GetConstructor method to obtain this  
        ' constructor. 
        Dim ctorOfU As ConstructorInfo = _
            TypeBuilder.GetConstructor(SampleOfU, ctor)
        ilg.Emit(OpCodes.Newobj, ctorOfU)
        ' Store the instance in the local variable; load it 
        ' again, and load the parameter of genMethod.
        ' In order to store the value in the field of the 
        ' instance of Sample(Of U), it is necessary to have  
        ' a FieldInfo representing the field of the  
        ' constructed type. Use TypeBuilder.GetField to  
        ' obtain this FieldInfo. 
        Dim FieldOfU As FieldInfo = _
            TypeBuilder.GetField(SampleOfU, myField)
        ' Store the value in the field. 
        ilg.Emit(OpCodes.Stfld, FieldOfU)
        ' Load the instance, load the field value, box it 
        ' (specifying the type of the type parameter, U),  
        ' and print it.
        ilg.Emit(OpCodes.Ldfld, FieldOfU)
        ilg.Emit(OpCodes.Box, methodParams(0))
        Dim writeLineObj As MethodInfo = _
            GetType(Console).GetMethod("WriteLine", _
                New Type() {GetType(Object)})
        ilg.EmitCall(OpCodes.Call, writeLineObj, Nothing)

        ' Emit an entry point method; this must be in a 
        ' non-generic type. 
        Dim dummy As TypeBuilder = _
            myModule.DefineType("Dummy", _
                TypeAttributes.Class Or TypeAttributes.NotPublic)
        Dim entryPoint As MethodBuilder = _
            dummy.DefineMethod("Main", _
                MethodAttributes.Public Or MethodAttributes.Static, _
                Nothing, Nothing)
        ilg = entryPoint.GetILGenerator()
        ' In order to call the static generic method GM, it is 
        ' necessary to create a constructed type from the  
        ' generic type definition for Sample. This can be ANY 
        ' constructed type; in this case Sample(Of Integer) 
        ' is used. 
        Dim SampleOfInt As Type = _
        ' Next get a MethodInfo representing the static generic 
        ' method GM on type Sample(Of Integer). 
        Dim SampleOfIntGM As MethodInfo = _
            TypeBuilder.GetMethod(SampleOfInt, genMethod)
        ' Next get a MethodInfo for GM(Of String), which is the  
        ' instantiation of generic method GM that is called 
        ' by Sub Main. 
        Dim GMOfString As MethodInfo = _
        ' Finally, emit the call. Push a string onto 
        ' the stack, as the argument for the generic method.
        ilg.Emit(OpCodes.Ldstr, "Hello, world!")
        ilg.EmitCall(OpCodes.Call, GMOfString, Nothing)

        myAssembly.Save(myAsmName.Name & ".exe")

        Console.WriteLine(myAsmName.Name & ".exe has been saved.")

    End Sub  
End Class

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0