MethodBuilder.SetParameters(Type[]) Metoda

Definice

Nastaví počet a typy parametrů pro metodu.

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())

Parametry

parameterTypes
Type[]

Pole Type objektů představujících typy parametrů.

Výjimky

Aktuální metoda je obecná, ale nejedná se o definici obecné metody. To znamená, že IsGenericMethod vlastnost je true, ale vlastnost IsGenericMethodDefinition je false.

Příklady

Následující příklad kódu používá metodu DefineGenericParameters k vytvoření obecné metody. Metoda SetParameters se používá k zadání jednoho parametru metody, jehož typ bude určen prvním parametrem obecného typu. Metoda SetReturnType se používá k zadání návratového typu metody určeného druhým parametrem obecného typu.

Tento kód je součástí většího příkladu, který je k dispozici pro metodu 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))

Poznámky

Pokud jsou počet a typy parametrů známy při definování metody, lze je nastavit pomocí libovolného TypeBuilder.DefineMethod přetížení metody, která přijímá pole typů parametrů. Obecná metoda však může mít parametry, jejichž typy jsou určeny jedním nebo více vlastními parametry obecného typu, které nelze definovat, dokud není definována metoda. Tuto metodu použijte k nastavení typů parametrů v takovém případě.

Pokud má návratový typ volitelné nebo požadované vlastní modifikátory, například IsConst, použijte SetSignature(Type, Type[], Type[], Type[], Type[][], Type[][]) přetížení metody.

Volání této metody nahradí všechny typy parametrů, které byly nastaveny pomocí TypeBuilder.DefineMethod metody.

Platí pro

Viz také