MethodBuilder.SetReturnType(Type) Method

Definition

Sets the return type of the method.

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)

Parameters

returnType
Type

A Type object that represents the return type of the method.

Exceptions

The current method is generic, but is not a generic method definition. That is, the IsGenericMethod property is true, but the IsGenericMethodDefinition property is false.

Examples

The following code example uses the DefineGenericParameters method to make a method generic. The SetParameters method is used to give the method one parameter, whose type will be specified by the first generic type parameter. The SetReturnType method is used to give the method a return type, specified by the second generic type parameter.

This code is part of a larger example provided for the DefineGenericParameters method.

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

Remarks

Use this method to set the return type of a generic method, when the return type is specified by one of the generic type parameters of the method. If the return type has optional or required custom modifiers, such as IsConst, use the SetSignature(Type, Type[], Type[], Type[], Type[][], Type[][]) method overload.

Calling this method replaces a return type established using the TypeBuilder.DefineMethod method.

Applies to

See also