Export (0) Print
Expand All

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:
void SetSignature(
	Type^ returnType, 
	array<Type^>^ returnTypeRequiredCustomModifiers, 
	array<Type^>^ returnTypeOptionalCustomModifiers, 
	array<Type^>^ parameterTypes, 
	array<array<Type^>^>^ parameterTypeRequiredCustomModifiers, 
	array<array<Type^>^>^ parameterTypeOptionalCustomModifiers
)

Parameters

returnType
Type: System::Type

The return type of the method.

returnTypeRequiredCustomModifiers
Type: array<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 nullptr.

returnTypeOptionalCustomModifiers
Type: array<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 nullptr.

parameterTypes
Type: array<System::Type>

The types of the parameters of the method.

parameterTypeRequiredCustomModifiers
Type: array<array<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 nullptr instead of an array of types. If none of the parameters have required custom modifiers, specify nullptr instead of an array of arrays.

parameterTypeOptionalCustomModifiers
Type: array<array<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 nullptr instead of an array of types. If none of the parameters have optional custom modifiers, specify nullptr instead of an array of arrays.

ExceptionCondition
InvalidOperationException

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 Ildasm.exe (IL Disassembler), 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 nullptr 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 Ildasm.exe (IL 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.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft