MethodBuilder.SetParameters(Type[]) 方法

定义

为方法设置参数的数量和类型。

public:
 void SetParameters(... cli::array <Type ^> ^ parameterTypes);
public void SetParameters (params Type[] parameterTypes);
member this.SetParameters : Type[] -> unit
Public Sub SetParameters (ParamArray parameterTypes As Type())

参数

parameterTypes
Type[]

表示参数类型的 Type 对象的数组。

例外

当前方法是泛型方法,但不是泛型方法定义。 也就是说,属性 IsGenericMethodtrue,但属性 IsGenericMethodDefinitionfalse

示例

下面的代码示例使用 DefineGenericParameters 方法使方法泛型。 方法 SetParameters 用于为方法提供一个参数,其类型将由第一个泛型类型参数指定。 方法 SetReturnType 用于为方法提供由第二个泛型类型参数指定的返回类型。

此代码是为 方法提供的更大示例的 DefineGenericParameters 一部分。

// 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.
//
array<String^>^ genericTypeNames = {"T", "U"};
array<GenericTypeParameterBuilder^>^ genericTypes =
    sampleMethodBuilder->DefineGenericParameters(
    genericTypeNames);
// 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);
' 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.
array<Type^>^ parameterTypes = {genericTypes[0]};
sampleMethodBuilder->SetParameters(parameterTypes);

// Set the return type for the method. The return type is
// specified by the second type parameter, U.
sampleMethodBuilder->SetReturnType(genericTypes[1]);
// 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]);
' 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))

注解

如果在定义方法时知道参数的数量和类型,则可以使用接受参数类型数组的方法的任何重载 TypeBuilder.DefineMethod 来设置这些参数。 但是,泛型方法可以具有其类型由其自己的一个或多个泛型类型参数指定的参数,在定义方法之后才能定义这些参数。 在这种情况下,使用此方法设置参数类型。

如果返回类型具有可选或必需的自定义修饰符,例如 IsConst,请使用 SetSignature(Type, Type[], Type[], Type[], Type[][], Type[][]) 方法重载。

调用此方法将替换使用 TypeBuilder.DefineMethod 方法设置的任何参数类型。

适用于

另请参阅