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 、メソッドに 1 つのパラメーターを指定するために使用されます。このパラメーターの型は、最初のジェネリック型パラメーターで指定されます。 メソッドは SetReturnType 、2 番目のジェネリック型パラメーターで指定された戻り値の型をメソッドに提供するために使用されます。

このコードは、 メソッドに対して提供されるより大きな例の 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 置き換えられます。

適用対象

こちらもご覧ください