MethodBuilder.SetParameters Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sets the number and types of parameters for a method.

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

Syntax

'Declaration
Public Sub SetParameters ( _
    ParamArray parameterTypes As Type() _
)
public void SetParameters(
    params Type[] parameterTypes
)

Parameters

  • parameterTypes
    Type: array<System.Type[]
    An array of Type objects representing the parameter types.

Exceptions

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

Remarks

If the number and types of the parameters are known when the method is defined, they can be set 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 the return type has optional or required custom modifiers, such as IsConst, use the SetSignature(Type, array<Type[], array<Type[], array<Type[], array<array<Type[][], array<array<Type[][]) method overload.

Calling this method replaces any parameter types that were set using the TypeBuilder.DefineMethod method.

Examples

The following example uses the DefineGenericParameters method to make a method generic. The SetParameters method is used to give the method one parameter, whose type will be specified by the first generic type parameter. The SetReturnType method is used to give the method a return type, specified by the second generic type parameter.

This code is part of a larger example provided for the DefineGenericParameters method.

' Defining generic parameters for the method makes it a
' generic method. By convention, type parameters are 
' single alphabetic characters. T and U are used here.
'
Dim typeParamNames() As String = {"T", "U"}
Dim typeParameters() As GenericTypeParameterBuilder = _
    demoMethod.DefineGenericParameters(typeParamNames)

' The second type parameter is constrained to be a 
' reference type.
typeParameters(1).SetGenericParameterAttributes( _
    GenericParameterAttributes.ReferenceTypeConstraint)


...


' Set parameter types for the method. The method takes
' one parameter, and its type is specified by the first
' type parameter, T.
Dim params() As Type = {typeParameters(0)}
demoMethod.SetParameters(params)

' Set the return type for the method. The return type is
' specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters(1))
// Defining generic parameters for the method makes it a
// generic method. By convention, type parameters are 
// single alphabetic characters. T and U are used here.
//
string[] typeParamNames = { "T", "U" };
GenericTypeParameterBuilder[] typeParameters =
    demoMethod.DefineGenericParameters(typeParamNames);

// The second type parameter is constrained to be a 
// reference type.
typeParameters[1].SetGenericParameterAttributes(
    GenericParameterAttributes.ReferenceTypeConstraint);


...


// Set parameter types for the method. The method takes
// one parameter, and its type is specified by the first
// type parameter, T.
Type[] parms = { typeParameters[0] };
demoMethod.SetParameters(parms);

// Set the return type for the method. The return type is
// specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters[1]);

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.