MethodBuilder.SetReturnType(Type) 方法

定義

設定方法的傳回型別。

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)

參數

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

備註

當方法的其中一個泛型型別參數指定傳回型別時,請使用這個方法來設定泛型方法的傳回型別。 如果傳回類型具有選擇性或必要的自定義修飾詞,例如 IsConst,請使用 SetSignature(Type, Type[], Type[], Type[], Type[][], Type[][]) 方法多載。

呼叫這個方法會取代使用 方法建立的傳回型別 TypeBuilder.DefineMethod

適用於

另請參閱