MethodBuilder.SetReturnType(Type) Metodo

Definizione

Imposta il tipo restituito del metodo.

public:
 void SetReturnType(Type ^ returnType);
public void SetReturnType (Type? returnType);
public void SetReturnType (Type returnType);
member this.SetReturnType : Type -> unit
Public Sub SetReturnType (returnType As Type)

Parametri

returnType
Type

Oggetto Type che rappresenta il tipo restituito del metodo.

Eccezioni

Il metodo corrente è un metodo generico, ma non una definizione di metodo generico. Questo significa che la proprietà IsGenericMethod è true, ma la proprietà IsGenericMethodDefinition è false.

Esempio

Nell'esempio di codice seguente viene usato il DefineGenericParameters metodo per rendere generico un metodo. Il SetParameters metodo viene usato per assegnare al metodo un parametro, il cui tipo verrà specificato dal primo parametro di tipo generico. Il SetReturnType metodo viene usato per assegnare al metodo un tipo restituito, specificato dal secondo parametro di tipo generico.

Questo codice fa parte di un esempio più grande fornito per il DefineGenericParameters metodo.

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

Commenti

Utilizzare questo metodo per impostare il tipo restituito di un metodo generico, quando il tipo restituito viene specificato da uno dei parametri di tipo generico del metodo. Se il tipo restituito ha modificatori personalizzati facoltativi o obbligatori, ad esempio IsConst, usare l'overload del SetSignature(Type, Type[], Type[], Type[], Type[][], Type[][]) metodo.

La chiamata a questo metodo sostituisce un tipo restituito stabilito usando il TypeBuilder.DefineMethod metodo .

Si applica a

Vedi anche