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 物件的陣列,代參數類型。

例外狀況

目前的方法是泛型,但不是泛型方法定義。 亦即,IsGenericMethod 屬性是 true,但 IsGenericMethodDefinition 屬性是 false

範例

下列程式代碼範例會 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 方法設定的任何參數類型。

適用於

另請參閱