ModuleBuilder.DefineGlobalMethod Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Definiuje metodę globalną.
Przeciążenia
DefineGlobalMethod(String, MethodAttributes, Type, Type[]) |
Definiuje metodę globalną o określonej nazwie, atrybutach, typie zwracanym i typie parametrów. |
DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[]) |
Definiuje metodę globalną o określonej nazwie, atrybutach, konwencji wywoływania, typie zwracanym i typach parametrów. |
DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][]) |
Definiuje metodę globalną o określonej nazwie, atrybutach, konwencji wywoływania, typie zwracanym, modyfikatorach niestandardowych dla typu zwracanego, typów parametrów i niestandardowych modyfikatorów dla typów parametrów. |
DefineGlobalMethod(String, MethodAttributes, Type, Type[])
- Źródło:
- ModuleBuilder.cs
- Źródło:
- ModuleBuilder.cs
- Źródło:
- ModuleBuilder.cs
Definiuje metodę globalną o określonej nazwie, atrybutach, typie zwracanym i typie parametrów.
public:
System::Reflection::Emit::MethodBuilder ^ DefineGlobalMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, Type? returnType, Type[]? parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, Type returnType, Type[] parameterTypes);
member this.DefineGlobalMethod : string * System.Reflection.MethodAttributes * Type * Type[] -> System.Reflection.Emit.MethodBuilder
Public Function DefineGlobalMethod (name As String, attributes As MethodAttributes, returnType As Type, parameterTypes As Type()) As MethodBuilder
Parametry
- name
- String
Nazwa metody. name
nie może zawierać osadzonych wartości null.
- attributes
- MethodAttributes
Atrybuty metody . attributes
musi zawierać wartość Static.
- returnType
- Type
Zwracany typ metody.
- parameterTypes
- Type[]
Typy parametrów metody.
Zwraca
Zdefiniowana metoda globalna.
Wyjątki
Metoda nie jest statyczna. Oznacza to, attributes
że nie zawiera Staticelementu .
-lub-
Długość name
to zero
-lub-
Element w tablicy Type to null
.
name
to null
.
CreateGlobalFunctions() został wcześniej wywołany.
Przykłady
W poniższym przykładzie pokazano użycie DefineGlobalMethod
metody do utworzenia metody niezależnej od typu powiązanej z bieżącym ModuleBuilderelementem . Po utworzeniu metody globalnej należy wywołać metodę w CreateGlobalFunctions celu jej ukończenia.
AppDomain^ currentDomain;
AssemblyName^ myAssemblyName;
MethodBuilder^ myMethodBuilder = nullptr;
ILGenerator^ myILGenerator;
// Get the current application domain for the current thread.
currentDomain = AppDomain::CurrentDomain;
myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "TempAssembly";
// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder =
currentDomain->DefineDynamicAssembly(
myAssemblyName, AssemblyBuilderAccess::RunAndSave );
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule" );
// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder->DefineGlobalMethod(
"MyMethod1", (MethodAttributes)(MethodAttributes::Static | MethodAttributes::Public),
nullptr, nullptr );
myILGenerator = myMethodBuilder->GetILGenerator();
myILGenerator->EmitWriteLine( "Hello World from global method." );
myILGenerator->Emit( OpCodes::Ret );
// Fix up the 'TempModule' module .
myModuleBuilder->CreateGlobalFunctions();
AppDomain currentDomain;
AssemblyName myAssemblyName;
MethodBuilder myMethodBuilder=null;
ILGenerator myILGenerator;
// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";
// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder =
currentDomain.DefineDynamicAssembly
(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod
("MyMethod1",MethodAttributes.Static|MethodAttributes.Public,
null,null);
myILGenerator = myMethodBuilder.GetILGenerator();
myILGenerator.EmitWriteLine("Hello World from global method.");
myILGenerator.Emit(OpCodes.Ret);
// Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions();
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName
Dim myMethodBuilder As MethodBuilder = Nothing
Dim myILGenerator As ILGenerator
' Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain
myAssemblyName = New AssemblyName()
myAssemblyName.Name = "TempAssembly"
' Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _
AssemblyBuilderAccess.RunAndSave)
' Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")
' Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod("MyMethod1", MethodAttributes.Static _
Or MethodAttributes.Public, Nothing, Nothing)
myILGenerator = myMethodBuilder.GetILGenerator()
myILGenerator.EmitWriteLine("Hello World from global method.")
myILGenerator.Emit(OpCodes.Ret)
' Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions()
Uwagi
Metoda globalna zdefiniowana przez tę metodę nie może być użyteczna, dopóki nie wywołasz metody CreateGlobalFunctions.
Uwaga
Począwszy od .NET Framework dodatku Service Pack 2.0, ten element członkowski nie wymaga ReflectionPermission już flagiReflectionPermissionFlag.ReflectionEmit. (Zobacz Problemy z zabezpieczeniami w emitu odbicia). Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.
Dotyczy
DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[])
- Źródło:
- ModuleBuilder.cs
- Źródło:
- ModuleBuilder.cs
- Źródło:
- ModuleBuilder.cs
Definiuje metodę globalną o określonej nazwie, atrybutach, konwencji wywoływania, typie zwracanym i typach parametrów.
public:
System::Reflection::Emit::MethodBuilder ^ DefineGlobalMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes);
member this.DefineGlobalMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] -> System.Reflection.Emit.MethodBuilder
Public Function DefineGlobalMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type()) As MethodBuilder
Parametry
- name
- String
Nazwa metody. name
nie może zawierać osadzonych wartości null.
- attributes
- MethodAttributes
Atrybuty metody . attributes
musi zawierać wartość Static.
- callingConvention
- CallingConventions
Konwencja wywoływania metody .
- returnType
- Type
Zwracany typ metody.
- parameterTypes
- Type[]
Typy parametrów metody.
Zwraca
Zdefiniowana metoda globalna.
Wyjątki
Metoda nie jest statyczna. Oznacza to, attributes
że nie zawiera Staticelementu .
-lub-
Element w tablicy Type to null
.
name
to null
.
CreateGlobalFunctions() został wcześniej wywołany.
Przykłady
Poniższy przykład kodu ilustruje użycie DefineGlobalMethod
metody do utworzenia metody niezależnej od typu powiązanej z bieżącym ModuleBuilderelementem . Po utworzeniu metody globalnej należy wywołać metodę w CreateGlobalFunctions celu jej ukończenia.
AppDomain^ currentDomain;
AssemblyName^ myAssemblyName;
MethodBuilder^ myMethodBuilder = nullptr;
ILGenerator^ myILGenerator;
// Get the current application domain for the current thread.
currentDomain = AppDomain::CurrentDomain;
myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "TempAssembly";
// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder =
currentDomain->DefineDynamicAssembly(
myAssemblyName, AssemblyBuilderAccess::RunAndSave );
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule" );
// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder->DefineGlobalMethod(
"MyMethod1", (MethodAttributes)(MethodAttributes::Static | MethodAttributes::Public),
nullptr, nullptr );
myILGenerator = myMethodBuilder->GetILGenerator();
myILGenerator->EmitWriteLine( "Hello World from global method." );
myILGenerator->Emit( OpCodes::Ret );
// Fix up the 'TempModule' module .
myModuleBuilder->CreateGlobalFunctions();
AppDomain currentDomain;
AssemblyName myAssemblyName;
MethodBuilder myMethodBuilder=null;
ILGenerator myILGenerator;
// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";
// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder =
currentDomain.DefineDynamicAssembly
(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod
("MyMethod1",MethodAttributes.Static|MethodAttributes.Public,
null,null);
myILGenerator = myMethodBuilder.GetILGenerator();
myILGenerator.EmitWriteLine("Hello World from global method.");
myILGenerator.Emit(OpCodes.Ret);
// Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions();
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName
Dim myMethodBuilder As MethodBuilder = Nothing
Dim myILGenerator As ILGenerator
' Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain
myAssemblyName = New AssemblyName()
myAssemblyName.Name = "TempAssembly"
' Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _
AssemblyBuilderAccess.RunAndSave)
' Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")
' Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod("MyMethod1", MethodAttributes.Static _
Or MethodAttributes.Public, Nothing, Nothing)
myILGenerator = myMethodBuilder.GetILGenerator()
myILGenerator.EmitWriteLine("Hello World from global method.")
myILGenerator.Emit(OpCodes.Ret)
' Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions()
Uwagi
Nie można użyć metody globalnej zdefiniowanej przez tę metodę do momentu wywołania metody CreateGlobalFunctions.
Uwaga
Począwszy od .NET Framework dodatku Service Pack 2.0, ten element członkowski nie wymaga ReflectionPermission już flagiReflectionPermissionFlag.ReflectionEmit. (Zobacz Problemy z zabezpieczeniami w emitu odbicia). Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.
Dotyczy
DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])
- Źródło:
- ModuleBuilder.cs
- Źródło:
- ModuleBuilder.cs
- Źródło:
- ModuleBuilder.cs
Definiuje metodę globalną o określonej nazwie, atrybutach, konwencji wywoływania, typie zwracanym, modyfikatorach niestandardowych dla typu zwracanego, typów parametrów i niestandardowych modyfikatorów dla typów parametrów.
public:
System::Reflection::Emit::MethodBuilder ^ DefineGlobalMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ requiredReturnTypeCustomModifiers, cli::array <Type ^> ^ optionalReturnTypeCustomModifiers, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ requiredParameterTypeCustomModifiers, cli::array <cli::array <Type ^> ^> ^ optionalParameterTypeCustomModifiers);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? requiredReturnTypeCustomModifiers, Type[]? optionalReturnTypeCustomModifiers, Type[]? parameterTypes, Type[][]? requiredParameterTypeCustomModifiers, Type[][]? optionalParameterTypeCustomModifiers);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers);
member this.DefineGlobalMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type[] * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.MethodBuilder
Public Function DefineGlobalMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, requiredReturnTypeCustomModifiers As Type(), optionalReturnTypeCustomModifiers As Type(), parameterTypes As Type(), requiredParameterTypeCustomModifiers As Type()(), optionalParameterTypeCustomModifiers As Type()()) As MethodBuilder
Parametry
- name
- String
Nazwa metody. name
nie może zawierać osadzonych znaków null.
- attributes
- MethodAttributes
Atrybuty metody . attributes
musi zawierać wartość Static.
- callingConvention
- CallingConventions
Konwencja wywoływania metody .
- returnType
- Type
Zwracany typ metody.
- requiredReturnTypeCustomModifiers
- Type[]
Tablica typów reprezentujących wymagane modyfikatory niestandardowe dla typu zwracanego, takiego jak IsConst lub IsBoxed. Jeśli typ zwracany nie ma wymaganych modyfikatorów niestandardowych, określ null
wartość .
- optionalReturnTypeCustomModifiers
- Type[]
Tablica typów reprezentujących opcjonalne modyfikatory niestandardowe dla typu zwracanego, takiego jak IsConst lub IsBoxed. Jeśli typ zwracany nie ma opcjonalnych modyfikatorów niestandardowych, określ null
wartość .
- parameterTypes
- Type[]
Typy parametrów metody.
- requiredParameterTypeCustomModifiers
- Type[][]
Tablica tablic typów. Każda tablica typów reprezentuje wymagane modyfikatory niestandardowe dla odpowiedniego parametru metody globalnej. Jeśli określony argument nie ma wymaganych modyfikatorów niestandardowych, określ null
zamiast tablicy typów. Jeśli metoda globalna nie ma argumentów lub jeśli żaden z argumentów nie wymagał modyfikatorów niestandardowych, określ null
zamiast tablicy tablic.
- optionalParameterTypeCustomModifiers
- Type[][]
Tablica tablic typów. Każda tablica typów reprezentuje opcjonalne modyfikatory niestandardowe dla odpowiedniego parametru. Jeśli określony argument nie ma opcjonalnych modyfikatorów niestandardowych, określ null
zamiast tablicy typów. Jeśli metoda globalna nie ma argumentów lub jeśli żaden z argumentów nie ma opcjonalnych modyfikatorów niestandardowych, określ null
zamiast tablicy tablic.
Zwraca
Zdefiniowana metoda globalna.
Wyjątki
Metoda nie jest statyczna. Oznacza to, attributes
że nie zawiera Staticelementu .
-lub-
Element w tablicy Type to null
.
name
to null
.
Metoda CreateGlobalFunctions() została wcześniej wywołana.
Uwagi
To przeciążenie jest udostępniane projektantom zarządzanych kompilatorów.
Nie można użyć metody globalnej zdefiniowanej przez tę metodę do momentu wywołania metody CreateGlobalFunctions.
Uwaga
Począwszy od .NET Framework dodatku Service Pack 2.0, ten element członkowski nie wymaga ReflectionPermission już flagiReflectionPermissionFlag.ReflectionEmit. (Zobacz Problemy z zabezpieczeniami w emitu odbicia). Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.
Dotyczy
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla