ModuleBuilder.GetArrayMethod Méthode
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 la méthode nommée sur une classe Array.
public:
System::Reflection::MethodInfo ^ GetArrayMethod(Type ^ arrayClass, System::String ^ methodName, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.MethodInfo GetArrayMethod (Type arrayClass, string methodName, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes);
public System.Reflection.MethodInfo GetArrayMethod (Type arrayClass, string methodName, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes);
member this.GetArrayMethod : Type * string * System.Reflection.CallingConventions * Type * Type[] -> System.Reflection.MethodInfo
Public Function GetArrayMethod (arrayClass As Type, methodName As String, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type()) As MethodInfo
- arrayClass
- Type
Classe Array.
- methodName
- String
Nom d'une méthode sur la classe Array.
- callingConvention
- CallingConventions
Convention d’appel de la méthode.
- returnType
- Type
Type de retour de la méthode.
- parameterTypes
- Type[]
Types des paramètres de la méthode.
Méthode nommée sur une classe Array.
arrayClass
n'est pas un tableau.
arrayClass
ou methodName
est null
.
L’exemple suivant montre comment utiliser GetArrayMethod pour obtenir le MethodInfo correspondant à une méthode qui retourne une valeur de tableau.
// Define a dynamic module in "TempAssembly" assembly.
ModuleBuilder^ myModuleBuilder = myAssemblyBuilder->
DefineDynamicModule( "TempModule" );
// Define a runtime class with specified name and attributes.
TypeBuilder^ myTypeBuilder = myModuleBuilder->DefineType(
"TempClass", TypeAttributes::Public );
array<Type^>^ paramArray = { Array::typeid };
// Add 'SortArray' method to the class, with the given signature.
MethodBuilder^ myMethod = myTypeBuilder->DefineMethod( "SortArray",
MethodAttributes::Public, Array::typeid, paramArray );
array<Type^>^ myArrayClass = gcnew array<Type^>( 1 );
array<Type^>^ parameterTypes = { Array::typeid };
// Get the 'MethodInfo' object corresponding to 'Sort' method of 'Array' class.
MethodInfo^ myMethodInfo = myModuleBuilder->GetArrayMethod(
myArrayClass->GetType(), "Sort", CallingConventions::Standard,
nullptr, parameterTypes );
// Get the token corresponding to 'Sort' method of 'Array' class.
MethodToken myMethodToken = myModuleBuilder->GetArrayMethodToken(
myArrayClass->GetType(), "Sort", CallingConventions::Standard,
nullptr, parameterTypes );
Console::WriteLine( "Token used by module to identify the 'Sort' method"
+ " of 'Array' class is : {0:x} ", myMethodToken.Token );
ILGenerator^ methodIL = myMethod->GetILGenerator();
methodIL->Emit( OpCodes::Ldarg_1 );
methodIL->Emit( OpCodes::Call, myMethodInfo );
methodIL->Emit( OpCodes::Ldarg_1 );
methodIL->Emit( OpCodes::Ret );
// Complete the creation of type.
myTypeBuilder->CreateType();
// Define a dynamic module in "TempAssembly" assembly.
ModuleBuilder myModuleBuilder = myAssemblyBuilder.
DefineDynamicModule("TempModule");
// Define a runtime class with specified name and attributes.
TypeBuilder myTypeBuilder = myModuleBuilder.DefineType
("TempClass",TypeAttributes.Public);
Type[] paramArray = {typeof(Array)};
// Add 'SortArray' method to the class, with the given signature.
MethodBuilder myMethod = myTypeBuilder.DefineMethod("SortArray",
MethodAttributes.Public,typeof(Array),paramArray);
Type[] myArrayClass = new Type[1];
Type[] parameterTypes = {typeof(Array)};
// Get the 'MethodInfo' object corresponding to 'Sort' method of 'Array' class.
MethodInfo myMethodInfo=myModuleBuilder.GetArrayMethod(
myArrayClass.GetType(),"Sort",CallingConventions.Standard,
null,parameterTypes);
// Get the token corresponding to 'Sort' method of 'Array' class.
MethodToken myMethodToken=myModuleBuilder.GetArrayMethodToken(
myArrayClass.GetType(),"Sort",CallingConventions.Standard,
null,parameterTypes);
Console.WriteLine("Token used by module to identify the 'Sort' method"
+ " of 'Array' class is : {0:x} ",myMethodToken.Token);
ILGenerator methodIL = myMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldarg_1);
methodIL.Emit(OpCodes.Call,myMethodInfo);
methodIL.Emit(OpCodes.Ldarg_1);
methodIL.Emit(OpCodes.Ret);
// Complete the creation of type.
myTypeBuilder.CreateType();
' Define a dynamic module in "TempAssembly" assembly.
Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")
' Define a runtime class with specified name and attributes.
Dim myTypeBuilder As TypeBuilder = _
myModuleBuilder.DefineType("TempClass", TypeAttributes.Public)
Dim myParamArray() As Type = New Type() {GetType(Array)}
' Add 'SortArray' method to the class, with the given signature.
Dim myMethod As MethodBuilder = _
myTypeBuilder.DefineMethod("SortArray", MethodAttributes.Public, _
GetType(Array), myParamArray)
Dim myArrayClass(0) As Type
Dim parameterTypes() As Type = New Type() {GetType(Array)}
' Get the 'MethodInfo' object corresponding to 'Sort' method of 'Array' class.
Dim myMethodInfo As MethodInfo = _
myModuleBuilder.GetArrayMethod(myArrayClass.GetType(), "Sort", _
CallingConventions.Standard, Nothing, parameterTypes)
' Get the token corresponding to 'Sort' method of 'Array' class.
Dim myMethodToken As MethodToken = _
myModuleBuilder.GetArrayMethodToken(myArrayClass.GetType(), _
"Sort", CallingConventions.Standard, Nothing, parameterTypes)
Console.WriteLine("Token used by module to identify the 'Sort' method" + _
" of 'Array' class is : {0:x} ", myMethodToken.Token)
Dim methodIL As ILGenerator = myMethod.GetILGenerator()
methodIL.Emit(OpCodes.Ldarg_1)
methodIL.Emit(OpCodes.Call, myMethodInfo)
methodIL.Emit(OpCodes.Ldarg_1)
methodIL.Emit(OpCodes.Ret)
' Complete the creation of type.
myTypeBuilder.CreateType()
GetArrayMethod
est utile lorsque vous disposez d’un tableau d’un type dont la définition n’est pas terminée et que vous souhaitez accéder aux méthodes définies sur Array. Par exemple, vous pouvez définir un type et définir une méthode qui prend un tableau du type en tant que paramètre. Pour accéder aux éléments du tableau, vous devez appeler des méthodes de la Array classe .
Produit | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided), 2.1 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :