MethodBuilder.SetReturnType(Type) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Définit le type de retour de la méthode.
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)
Paramètres
Exceptions
La méthode actuelle est générique, mais n’est pas une définition de méthode générique. Autrement dit, la propriété IsGenericMethod a la valeur true
, mais la propriété IsGenericMethodDefinition a la valeur false
.
Exemples
L’exemple de code suivant utilise la DefineGenericParameters méthode pour rendre une méthode générique. La SetParameters méthode est utilisée pour donner à la méthode un paramètre, dont le type sera spécifié par le premier paramètre de type générique. La SetReturnType méthode est utilisée pour donner à la méthode un type de retour, spécifié par le deuxième paramètre de type générique.
Ce code fait partie d’un exemple plus large fourni pour la DefineGenericParameters méthode .
// 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))
Remarques
Utilisez cette méthode pour définir le type de retour d’une méthode générique, lorsque le type de retour est spécifié par l’un des paramètres de type générique de la méthode. Si le type de retour a des modificateurs personnalisés facultatifs ou obligatoires, tels que IsConst, utilisez la surcharge de méthode SetSignature(Type, Type[], Type[], Type[], Type[][], Type[][]) .
L’appel de cette méthode remplace un type de retour établi à l’aide de la TypeBuilder.DefineMethod méthode .
S’applique à
Voir aussi
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour