MethodBuilder.MakeGenericMethod(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.
Retourne une méthode générique construite à partir de la définition de méthode générique actuelle à l’aide des arguments de type générique spécifiés.
public:
override System::Reflection::MethodInfo ^ MakeGenericMethod(... cli::array <Type ^> ^ typeArguments);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")>]
override this.MakeGenericMethod : Type[] -> System.Reflection.MethodInfo
override this.MakeGenericMethod : Type[] -> System.Reflection.MethodInfo
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")>]
override this.MakeGenericMethod : Type[] -> System.Reflection.MethodInfo
Public Overrides Function MakeGenericMethod (ParamArray typeArguments As Type()) As MethodInfo
Paramètres
- typeArguments
- Type[]
Tableau d’objets Type qui représentent les arguments de type de la méthode générique.
Retours
Représentant MethodInfo la méthode générique construite à partir de la définition de méthode générique actuelle à l’aide des arguments de type générique spécifiés.
- Attributs
Exemples
L’exemple de code suivant crée une méthode construite à partir d’une définition de méthode générique incomplète dans un type incomplet.
L’exemple crée un assembly et un module temporaires avec un seul type, ajoute une méthode Met rend la méthode générique en ajoutant un paramètre de type T à l’aide de la DefineGenericParameters méthode. Le paramètre de type est utilisé comme type du paramètre de la méthode, ainsi que comme type de retour. La définition de méthode générique n’est pas donnée à un corps et le type englobant n’est pas terminé. La méthode MakeGenericMethod est ensuite utilisée pour rendre la méthode construite M<String> (M(Of String) dans Visual Basic). L’exemple de code n’a pas de sortie, car la sous-classe retournée MethodInfo par la méthode n’autorise pas la MakeGenericMethod réflexion sur ses paramètres.
Note
Pour obtenir un autre exemple de code qui utilise MakeGenericMethod, consultez DefineGenericParameters. MakeGenericMethod est également utilisé en grande partie lors de l’émission de code qui utilise des types génériques. Découvrez comment : définir une méthode générique avec l’émission de réflexion.
using System;
using System.Reflection;
using System.Reflection.Emit;
class Example
{
public static void Main()
{
// Define a transient dynamic assembly (only to run, not
// to save) with one module and a type "Test".
//
AssemblyName aName = new AssemblyName("MyDynamic");
AssemblyBuilder ab =
AppDomain.CurrentDomain.DefineDynamicAssembly(
aName,
AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
TypeBuilder tb = mb.DefineType("Test");
// Add a public static method "M" to Test, and make it a
// generic method with one type parameter named "T").
//
MethodBuilder meb = tb.DefineMethod("M",
MethodAttributes.Public | MethodAttributes.Static);
GenericTypeParameterBuilder[] typeParams =
meb.DefineGenericParameters(new string[] { "T" });
// Give the method one parameter, of type T, and a
// return type of T.
meb.SetParameters(typeParams);
meb.SetReturnType(typeParams[0]);
// Create a MethodInfo for M<string>, which can be used in
// emitted code. This is possible even though the method
// does not yet have a body, and the enclosing type is not
// created.
MethodInfo mi = meb.MakeGenericMethod(typeof(string));
// Note that this is actually a subclass of MethodInfo,
// which has rather limited capabilities -- for
// example, you cannot reflect on its parameters.
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Class Example
Public Shared Sub Main()
' Define a transient dynamic assembly (only to run, not
' to save) with one module and a type "Test".
'
Dim aName As AssemblyName = New AssemblyName("MyDynamic")
Dim ab As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly( _
aName, _
AssemblyBuilderAccess.Run)
Dim mb As ModuleBuilder = ab.DefineDynamicModule(aName.Name)
Dim tb As TypeBuilder = mb.DefineType("Test")
' Add a Public Shared method "M" to Test, and make it a
' generic method with one type parameter named "T").
'
Dim meb As MethodBuilder = tb.DefineMethod("M", _
MethodAttributes.Public Or MethodAttributes.Static)
Dim typeParams() As GenericTypeParameterBuilder = _
meb.DefineGenericParameters(New String() { "T" })
' Give the method one parameter, of type T, and a
' return type of T.
meb.SetParameters(typeParams)
meb.SetReturnType(typeParams(0))
' Create a MethodInfo for M(Of String), which can be used
' in emitted code. This is possible even though the method
' does not yet have a body, and the enclosing type is not
' created.
Dim mi As MethodInfo = _
meb.MakeGenericMethod(GetType(String))
' Note that this is actually a subclass of MethodInfo,
' which has rather limited capabilities -- for
' example, you cannot reflect on its parameters.
End Sub
End Class
Remarques
Lorsque vous émettez du code dynamique, vous devrez peut-être émettre un appel à une méthode construite à partir de la définition de méthode générique représentée par un MethodBuilder, avant la fin du type englobant. Vous pouvez utiliser la MakeGenericMethod méthode pour créer une MethodInfo telle méthode construite et l’utiliser MethodInfo dans l’appel émis.