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.
//
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.
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 方法設定的任何參數型別。

適用於

另請參閱