名前、属性、戻り値の型、およびパラメータ型を指定して、グローバル メソッドを定義します。
Overloads Public Function DefineGlobalMethod( _
ByVal name As String, _ ByVal attributes As MethodAttributes, _ ByVal returnType As Type, _ ByVal parameterTypes() As Type _) As MethodBuilder
[C#]
public MethodBuilder DefineGlobalMethod(stringname,MethodAttributesattributes,TypereturnType,Type[] parameterTypes);
[C++]
public: MethodBuilder* DefineGlobalMethod(String* name,MethodAttributesattributes,Type* returnType,Type* parameterTypes[]);
[JScript]
public function DefineGlobalMethod(
name : String,attributes : MethodAttributes,returnType : Type,parameterTypes : Type[]) : MethodBuilder;
パラメータ
- name
メソッドの名前。name に null を埋め込むことはできません。 - attributes
メソッドの属性。 - returnType
メソッドの戻り値の型。 - parameterTypes
メソッドのパラメータの型。
戻り値
定義されたグローバル メソッドを返します。
例外
例外の種類 | 条件 |
---|---|
ArgumentException | このメソッドは静的ではありません。
または name の長さが 0 です。 |
ArgumentNullException | name が null 参照 (Visual Basic では Nothing) です。 |
InvalidOperationException | CreateGlobalFunctions は既に呼び出されています。 |
解説
実装時の注意: このメソッドで定義したグローバル メソッドを使用するには、 CreateGlobalFunctions を呼び出す必要があります。
使用例
[Visual Basic, C#, C++] 次のコード例は、 DefineGlobalMethod を使用して、現在の ModuleBuilder に結び付けられた、型に依存しないメソッドを作成する方法を示しています。グローバル メソッドを作成した後、これを完了するためには、 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()
[C#]
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();
[C++]
AppDomain* currentDomain;
AssemblyName* myAssemblyName;
MethodBuilder* myMethodBuilder=0;
ILGenerator* myILGenerator;
// Get the current application ___domain for the current thread.
currentDomain = AppDomain::CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName->Name = S"TempAssembly";
// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = currentDomain->DefineDynamicAssembly(
myAssemblyName,
AssemblyBuilderAccess::RunAndSave);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule(S"TempModule");
// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder->DefineGlobalMethod(
S"MyMethod1",
static_cast<MethodAttributes>(MethodAttributes::Static | MethodAttributes::Public),
0,
0);
myILGenerator = myMethodBuilder->GetILGenerator();
myILGenerator->EmitWriteLine(S"Hello World from global method.");
myILGenerator->Emit(OpCodes::Ret);
// Fix up the 'TempModule' module .
myModuleBuilder->CreateGlobalFunctions();
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
.NET Framework セキュリティ:
- ReflectionPermission SecurityAction.Demand、ReflectionEmit
参照
ModuleBuilder クラス | ModuleBuilder メンバ | System.Reflection.Emit 名前空間 | ModuleBuilder.DefineGlobalMethod オーバーロードの一覧