ILGenerator.Emit メソッド (OpCode, ConstructorInfo)
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(True)> _ Public Overridable Sub Emit ( _ opcode As OpCode, _ con As ConstructorInfo _ )
Dim instance As ILGenerator Dim opcode As OpCode Dim con As ConstructorInfo instance.Emit(opcode, con)
- opcode

この命令値は、OpCodes 列挙体で定義されている値にします。
モジュールをポータブル実行可能 (PE) ファイルに永続化するときに、必要に応じて命令ストリームにパッチを当てることができるように、con の位置が記録されます。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, FieldInfo)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As ILGenerator Dim opcode As OpCode Dim field As FieldInfo instance.Emit(opcode, field)
- opcode

この命令値は、OpCodes 列挙体で定義されている値にします。モジュールを PE に永続化するときに、必要に応じて命令ストリームにパッチを当てることができるように、field の位置が記録されます。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, LocalBuilder)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As ILGenerator Dim opcode As OpCode Dim local As LocalBuilder instance.Emit(opcode, local)



Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

- opcode


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, SignatureHelper)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As ILGenerator Dim opcode As OpCode Dim signature As SignatureHelper instance.Emit(opcode, signature)
- opcode



Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド


ILGenerator.Emit メソッド (OpCode, Int64)
アセンブリ: mscorlib (mscorlib.dll 内)

- opcode


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, MethodInfo)
アセンブリ: mscorlib (mscorlib.dll 内)



この命令値は、OpCodes 列挙体で定義されている値にします。
モジュールを移植可能な実行可能ファイルに永続化するときに、必要に応じて命令ストリームにパッチを当てることができるように、meth の位置が記録されます。
meth がジェネリック メソッドを表している場合は、ジェネリック メソッド定義にする必要があります。つまり、MethodInfo.IsGenericMethodDefinition プロパティが true のメソッドとする必要があります。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, String)
アセンブリ: mscorlib (mscorlib.dll 内)



Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, Byte)
アセンブリ: mscorlib (mscorlib.dll 内)

- opcode


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, Int16)
アセンブリ: mscorlib (mscorlib.dll 内)

- opcode


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, Type)
アセンブリ: mscorlib (mscorlib.dll 内)




Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, Label[])
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As ILGenerator Dim opcode As OpCode Dim labels As Label() instance.Emit(opcode, labels)
- opcode

この命令値は、OpCodes 列挙体で定義されている値にします。
ラベルは、DefineLabel を使用して作成します。ストリーム内のラベルの位置は、MarkLabel を使用して確定します。1 バイトの命令を使用する場合、ラベルはストリーム内で最大 127 バイトのジャンプを表すことができます。instruction は、分岐命令を表す必要があります。分岐は相対的な命令であるため、label はフィックスアップ処理中に分岐への正しいオフセットに置き換えられます。

ジャンプ テーブルを持つ動的メソッドを作成する方法を次のコード例に示します。ジャンプ テーブルは、Label の配列を使用して作成します。
Imports System Imports System.Threading Imports System.Reflection Imports System.Reflection.Emit _ Class DynamicJumpTableDemo Public Shared Function BuildMyType() As Type Dim myDomain As AppDomain = Thread.GetDomain() Dim myAsmName As New AssemblyName() myAsmName.Name = "MyDynamicAssembly" Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _ AssemblyBuilderAccess.Run) Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyJumpTableDemo") Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("JumpTableDemo", _ TypeAttributes.Public) Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("SwitchMe", _ MethodAttributes.Public Or MethodAttributes.Static, _ GetType(String), New Type() {GetType(Integer)}) Dim myIL As ILGenerator = myMthdBuilder.GetILGenerator() Dim defaultCase As Label = myIL.DefineLabel() Dim endOfMethod As Label = myIL.DefineLabel() ' We are initializing our jump table. Note that the labels ' will be placed later using the MarkLabel method. Dim jumpTable() As Label = {myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel()} ' arg0, the number we passed, is pushed onto the stack. ' In this case, due to the design of the code sample, ' the value pushed onto the stack happens to match the ' index of the label (in IL terms, the index of the offset ' in the jump table). If this is not the case, such as ' when switching based on non-integer values, rules for the correspondence ' between the possible case values and each index of the offsets ' must be established outside of the ILGenerator.Emit calls, ' much as a compiler would. myIL.Emit(OpCodes.Ldarg_0) myIL.Emit(OpCodes.Switch, jumpTable) ' Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase) ' Case arg0 = 0 myIL.MarkLabel(jumpTable(0)) myIL.Emit(OpCodes.Ldstr, "are no bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 1 myIL.MarkLabel(jumpTable(1)) myIL.Emit(OpCodes.Ldstr, "is one banana") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 2 myIL.MarkLabel(jumpTable(2)) myIL.Emit(OpCodes.Ldstr, "are two bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 3 myIL.MarkLabel(jumpTable(3)) myIL.Emit(OpCodes.Ldstr, "are three bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 4 myIL.MarkLabel(jumpTable(4)) myIL.Emit(OpCodes.Ldstr, "are four bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Default case myIL.MarkLabel(defaultCase) myIL.Emit(OpCodes.Ldstr, "are many bananas") myIL.MarkLabel(endOfMethod) myIL.Emit(OpCodes.Ret) Return myTypeBuilder.CreateType() End Function 'BuildMyType Public Shared Sub Main() Dim myType As Type = BuildMyType() Console.Write("Enter an integer between 0 and 5: ") Dim theValue As Integer = Convert.ToInt32(Console.ReadLine()) Console.WriteLine("---") Dim myInstance As [Object] = Activator.CreateInstance(myType, New Object() {}) Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe", _ BindingFlags.InvokeMethod, Nothing, _ myInstance, New Object() {theValue})) End Sub 'Main End Class 'DynamicJumpTableDemo
using System; using System.Threading; using System.Reflection; using System.Reflection.Emit; class DynamicJumpTableDemo { public static Type BuildMyType() { AppDomain myDomain = Thread.GetDomain(); AssemblyName myAsmName = new AssemblyName(); myAsmName.Name = "MyDynamicAssembly"; AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule( "MyJumpTableDemo"); TypeBuilder myTypeBuilder = myModBuilder.DefineType("JumpTableDemo" , TypeAttributes.Public); MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("SwitchMe", MethodAttributes.Public | MethodAttributes.Static, typeof(string), new Type[] {typeof(int)}); ILGenerator myIL = myMthdBuilder.GetILGenerator(); Label defaultCase = myIL.DefineLabel(); Label endOfMethod = myIL.DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. Label[] jumpTable = new Label[] { myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel() }; // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the correspondence // between the possible case values and each index of the offsets // must be established outside of the ILGenerator.Emit calls, // much as a compiler would. myIL.Emit(OpCodes.Ldarg_0); myIL.Emit(OpCodes.Switch, jumpTable); // Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase); // Case arg0 = 0 myIL.MarkLabel(jumpTable[0]); myIL.Emit(OpCodes.Ldstr, "are no bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 1 myIL.MarkLabel(jumpTable[1]); myIL.Emit(OpCodes.Ldstr, "is one banana"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 2 myIL.MarkLabel(jumpTable[2]); myIL.Emit(OpCodes.Ldstr, "are two bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 3 myIL.MarkLabel(jumpTable[3]); myIL.Emit(OpCodes.Ldstr, "are three bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 4 myIL.MarkLabel(jumpTable[4]); myIL.Emit(OpCodes.Ldstr, "are four bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Default case myIL.MarkLabel(defaultCase); myIL.Emit(OpCodes.Ldstr, "are many bananas"); myIL.MarkLabel(endOfMethod); myIL.Emit(OpCodes.Ret); return myTypeBuilder.CreateType(); } public static void Main() { Type myType = BuildMyType(); Console.Write("Enter an integer between 0 and 5: "); int theValue = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("---"); Object myInstance = Activator.CreateInstance(myType, new object[0]); Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe" , BindingFlags.InvokeMethod, null, myInstance, new object[] {theValue})); } }
using namespace System; using namespace System::Threading; using namespace System::Reflection; using namespace System::Reflection::Emit; Type^ BuildMyType() { AppDomain^ myDomain = Thread::GetDomain(); AssemblyName^ myAsmName = gcnew AssemblyName; myAsmName->Name = "MyDynamicAssembly"; AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::Run ); ModuleBuilder^ myModBuilder = myAsmBuilder->DefineDynamicModule( "MyJumpTableDemo" ); TypeBuilder^ myTypeBuilder = myModBuilder->DefineType( "JumpTableDemo", TypeAttributes::Public ); array<Type^>^temp0 = {int::typeid}; MethodBuilder^ myMthdBuilder = myTypeBuilder->DefineMethod( "SwitchMe", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), String::typeid, temp0 ); ILGenerator^ myIL = myMthdBuilder->GetILGenerator(); Label defaultCase = myIL->DefineLabel(); Label endOfMethod = myIL->DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. array<Label>^jumpTable = gcnew array<Label>(5); jumpTable[ 0 ] = myIL->DefineLabel(); jumpTable[ 1 ] = myIL->DefineLabel(); jumpTable[ 2 ] = myIL->DefineLabel(); jumpTable[ 3 ] = myIL->DefineLabel(); jumpTable[ 4 ] = myIL->DefineLabel(); // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the correspondence // between the possible case values and each index of the offsets // must be established outside of the ILGenerator::Emit calls, // much as a compiler would. myIL->Emit( OpCodes::Ldarg_0 ); myIL->Emit( OpCodes::Switch, jumpTable ); // Branch on default case myIL->Emit( OpCodes::Br_S, defaultCase ); // Case arg0 = 0 myIL->MarkLabel( jumpTable[ 0 ] ); myIL->Emit( OpCodes::Ldstr, "are no bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 1 myIL->MarkLabel( jumpTable[ 1 ] ); myIL->Emit( OpCodes::Ldstr, "is one banana" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 2 myIL->MarkLabel( jumpTable[ 2 ] ); myIL->Emit( OpCodes::Ldstr, "are two bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 3 myIL->MarkLabel( jumpTable[ 3 ] ); myIL->Emit( OpCodes::Ldstr, "are three bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 4 myIL->MarkLabel( jumpTable[ 4 ] ); myIL->Emit( OpCodes::Ldstr, "are four bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Default case myIL->MarkLabel( defaultCase ); myIL->Emit( OpCodes::Ldstr, "are many bananas" ); myIL->MarkLabel( endOfMethod ); myIL->Emit( OpCodes::Ret ); return myTypeBuilder->CreateType(); } int main() { Type^ myType = BuildMyType(); Console::Write( "Enter an integer between 0 and 5: " ); int theValue = Convert::ToInt32( Console::ReadLine() ); Console::WriteLine( "---" ); Object^ myInstance = Activator::CreateInstance( myType, gcnew array<Object^>(0) ); array<Object^>^temp1 = {theValue}; Console::WriteLine( "Yes, there {0} today!", myType->InvokeMember( "SwitchMe", BindingFlags::InvokeMethod, nullptr, myInstance, temp1 ) ); }
import System .* ; import System.Threading .* ; import System.Reflection .* ; import System.Reflection.Emit .* ; class DynamicJumpTableDemo { public static Type BuildMyType() { AppDomain myDomain = System.Threading.Thread.GetDomain(); AssemblyName myAsmName = new AssemblyName(); myAsmName.set_Name("MyDynamicAssembly"); AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly (myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule ("MyJumpTableDemo"); TypeBuilder myTypeBuilder = myModBuilder.DefineType("JumpTableDemo" , TypeAttributes.Public); MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("SwitchMe" , MethodAttributes.Public | MethodAttributes.Static, String.class.ToType(),new Type[] { int.class.ToType() }); ILGenerator myIL = myMthdBuilder.GetILGenerator(); Label defaultCase = myIL.DefineLabel(); Label endOfMethod = myIL.DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. Label jumpTable[] = new Label[] { myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel() }; // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the // correspondence between the possible case values and each index // of the offsets must be established outside of the ILGenerator. // Emit calls, much as a compiler would. myIL.Emit(OpCodes.Ldarg_0); myIL.Emit(OpCodes.Switch, jumpTable); // Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase); // Case arg0 = 0 myIL.MarkLabel(jumpTable[0]); myIL.Emit(OpCodes.Ldstr, "are no bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 1 myIL.MarkLabel(jumpTable[1]); myIL.Emit(OpCodes.Ldstr, "is one banana"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 2 myIL.MarkLabel(jumpTable[2]); myIL.Emit(OpCodes.Ldstr, "are two bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 3 myIL.MarkLabel(jumpTable[3]); myIL.Emit(OpCodes.Ldstr, "are three bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 4 myIL.MarkLabel(jumpTable[4]); myIL.Emit(OpCodes.Ldstr, "are four bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Default case myIL.MarkLabel(defaultCase); myIL.Emit(OpCodes.Ldstr, "are many bananas"); myIL.MarkLabel(endOfMethod); myIL.Emit(OpCodes.Ret); return myTypeBuilder.CreateType(); } //BuildMyType public static void main(String[] args) { Type myType = BuildMyType(); Console.Write("Enter an integer between 0 and 5: "); int theValue = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("---"); Object myInstance = Activator.CreateInstance(myType,new Object[0]); Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe", BindingFlags.InvokeMethod, null, myInstance, new Object[]{(Int32)(theValue)})); } //main } //DynamicJumpTableDemo

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, Double)
アセンブリ: mscorlib (mscorlib.dll 内)



Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, SByte)
アセンブリ: mscorlib (mscorlib.dll 内)

- opcode


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, Label)
アセンブリ: mscorlib (mscorlib.dll 内)

- opcode

この命令値は、OpCodes 列挙体で定義されている値にします。
ラベルは、DefineLabel を使用して作成します。ストリーム内のラベルの位置は、MarkLabel を使用して確定します。1 バイトの命令を使用する場合、ラベルはストリーム内で最大 127 バイトのジャンプを表すことができます。instruction は、分岐命令を表す必要があります。分岐は相対的な命令であるため、label はフィックスアップ処理中に分岐への正しいオフセットに置き換えられます。

ジャンプ テーブルを持つ動的メソッドを作成する方法を次のコード例に示します。ジャンプ テーブルは、Label の配列を使用して作成します。
Imports System Imports System.Threading Imports System.Reflection Imports System.Reflection.Emit _ Class DynamicJumpTableDemo Public Shared Function BuildMyType() As Type Dim myDomain As AppDomain = Thread.GetDomain() Dim myAsmName As New AssemblyName() myAsmName.Name = "MyDynamicAssembly" Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _ AssemblyBuilderAccess.Run) Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyJumpTableDemo") Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("JumpTableDemo", _ TypeAttributes.Public) Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("SwitchMe", _ MethodAttributes.Public Or MethodAttributes.Static, _ GetType(String), New Type() {GetType(Integer)}) Dim myIL As ILGenerator = myMthdBuilder.GetILGenerator() Dim defaultCase As Label = myIL.DefineLabel() Dim endOfMethod As Label = myIL.DefineLabel() ' We are initializing our jump table. Note that the labels ' will be placed later using the MarkLabel method. Dim jumpTable() As Label = {myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel()} ' arg0, the number we passed, is pushed onto the stack. ' In this case, due to the design of the code sample, ' the value pushed onto the stack happens to match the ' index of the label (in IL terms, the index of the offset ' in the jump table). If this is not the case, such as ' when switching based on non-integer values, rules for the correspondence ' between the possible case values and each index of the offsets ' must be established outside of the ILGenerator.Emit calls, ' much as a compiler would. myIL.Emit(OpCodes.Ldarg_0) myIL.Emit(OpCodes.Switch, jumpTable) ' Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase) ' Case arg0 = 0 myIL.MarkLabel(jumpTable(0)) myIL.Emit(OpCodes.Ldstr, "are no bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 1 myIL.MarkLabel(jumpTable(1)) myIL.Emit(OpCodes.Ldstr, "is one banana") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 2 myIL.MarkLabel(jumpTable(2)) myIL.Emit(OpCodes.Ldstr, "are two bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 3 myIL.MarkLabel(jumpTable(3)) myIL.Emit(OpCodes.Ldstr, "are three bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 4 myIL.MarkLabel(jumpTable(4)) myIL.Emit(OpCodes.Ldstr, "are four bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Default case myIL.MarkLabel(defaultCase) myIL.Emit(OpCodes.Ldstr, "are many bananas") myIL.MarkLabel(endOfMethod) myIL.Emit(OpCodes.Ret) Return myTypeBuilder.CreateType() End Function 'BuildMyType Public Shared Sub Main() Dim myType As Type = BuildMyType() Console.Write("Enter an integer between 0 and 5: ") Dim theValue As Integer = Convert.ToInt32(Console.ReadLine()) Console.WriteLine("---") Dim myInstance As [Object] = Activator.CreateInstance(myType, New Object() {}) Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe", _ BindingFlags.InvokeMethod, Nothing, _ myInstance, New Object() {theValue})) End Sub 'Main End Class 'DynamicJumpTableDemo
using System; using System.Threading; using System.Reflection; using System.Reflection.Emit; class DynamicJumpTableDemo { public static Type BuildMyType() { AppDomain myDomain = Thread.GetDomain(); AssemblyName myAsmName = new AssemblyName(); myAsmName.Name = "MyDynamicAssembly"; AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule( "MyJumpTableDemo"); TypeBuilder myTypeBuilder = myModBuilder.DefineType("JumpTableDemo" , TypeAttributes.Public); MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("SwitchMe", MethodAttributes.Public | MethodAttributes.Static, typeof(string), new Type[] {typeof(int)}); ILGenerator myIL = myMthdBuilder.GetILGenerator(); Label defaultCase = myIL.DefineLabel(); Label endOfMethod = myIL.DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. Label[] jumpTable = new Label[] { myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel() }; // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the correspondence // between the possible case values and each index of the offsets // must be established outside of the ILGenerator.Emit calls, // much as a compiler would. myIL.Emit(OpCodes.Ldarg_0); myIL.Emit(OpCodes.Switch, jumpTable); // Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase); // Case arg0 = 0 myIL.MarkLabel(jumpTable[0]); myIL.Emit(OpCodes.Ldstr, "are no bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 1 myIL.MarkLabel(jumpTable[1]); myIL.Emit(OpCodes.Ldstr, "is one banana"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 2 myIL.MarkLabel(jumpTable[2]); myIL.Emit(OpCodes.Ldstr, "are two bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 3 myIL.MarkLabel(jumpTable[3]); myIL.Emit(OpCodes.Ldstr, "are three bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 4 myIL.MarkLabel(jumpTable[4]); myIL.Emit(OpCodes.Ldstr, "are four bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Default case myIL.MarkLabel(defaultCase); myIL.Emit(OpCodes.Ldstr, "are many bananas"); myIL.MarkLabel(endOfMethod); myIL.Emit(OpCodes.Ret); return myTypeBuilder.CreateType(); } public static void Main() { Type myType = BuildMyType(); Console.Write("Enter an integer between 0 and 5: "); int theValue = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("---"); Object myInstance = Activator.CreateInstance(myType, new object[0]); Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe" , BindingFlags.InvokeMethod, null, myInstance, new object[] {theValue})); } }
using namespace System; using namespace System::Threading; using namespace System::Reflection; using namespace System::Reflection::Emit; Type^ BuildMyType() { AppDomain^ myDomain = Thread::GetDomain(); AssemblyName^ myAsmName = gcnew AssemblyName; myAsmName->Name = "MyDynamicAssembly"; AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::Run ); ModuleBuilder^ myModBuilder = myAsmBuilder->DefineDynamicModule( "MyJumpTableDemo" ); TypeBuilder^ myTypeBuilder = myModBuilder->DefineType( "JumpTableDemo", TypeAttributes::Public ); array<Type^>^temp0 = {int::typeid}; MethodBuilder^ myMthdBuilder = myTypeBuilder->DefineMethod( "SwitchMe", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), String::typeid, temp0 ); ILGenerator^ myIL = myMthdBuilder->GetILGenerator(); Label defaultCase = myIL->DefineLabel(); Label endOfMethod = myIL->DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. array<Label>^jumpTable = gcnew array<Label>(5); jumpTable[ 0 ] = myIL->DefineLabel(); jumpTable[ 1 ] = myIL->DefineLabel(); jumpTable[ 2 ] = myIL->DefineLabel(); jumpTable[ 3 ] = myIL->DefineLabel(); jumpTable[ 4 ] = myIL->DefineLabel(); // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the correspondence // between the possible case values and each index of the offsets // must be established outside of the ILGenerator::Emit calls, // much as a compiler would. myIL->Emit( OpCodes::Ldarg_0 ); myIL->Emit( OpCodes::Switch, jumpTable ); // Branch on default case myIL->Emit( OpCodes::Br_S, defaultCase ); // Case arg0 = 0 myIL->MarkLabel( jumpTable[ 0 ] ); myIL->Emit( OpCodes::Ldstr, "are no bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 1 myIL->MarkLabel( jumpTable[ 1 ] ); myIL->Emit( OpCodes::Ldstr, "is one banana" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 2 myIL->MarkLabel( jumpTable[ 2 ] ); myIL->Emit( OpCodes::Ldstr, "are two bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 3 myIL->MarkLabel( jumpTable[ 3 ] ); myIL->Emit( OpCodes::Ldstr, "are three bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 4 myIL->MarkLabel( jumpTable[ 4 ] ); myIL->Emit( OpCodes::Ldstr, "are four bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Default case myIL->MarkLabel( defaultCase ); myIL->Emit( OpCodes::Ldstr, "are many bananas" ); myIL->MarkLabel( endOfMethod ); myIL->Emit( OpCodes::Ret ); return myTypeBuilder->CreateType(); } int main() { Type^ myType = BuildMyType(); Console::Write( "Enter an integer between 0 and 5: " ); int theValue = Convert::ToInt32( Console::ReadLine() ); Console::WriteLine( "---" ); Object^ myInstance = Activator::CreateInstance( myType, gcnew array<Object^>(0) ); array<Object^>^temp1 = {theValue}; Console::WriteLine( "Yes, there {0} today!", myType->InvokeMember( "SwitchMe", BindingFlags::InvokeMethod, nullptr, myInstance, temp1 ) ); }
import System .* ; import System.Threading .* ; import System.Reflection .* ; import System.Reflection.Emit .* ; class DynamicJumpTableDemo { public static Type BuildMyType() { AppDomain myDomain = System.Threading.Thread.GetDomain(); AssemblyName myAsmName = new AssemblyName(); myAsmName.set_Name("MyDynamicAssembly"); AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly (myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule ("MyJumpTableDemo"); TypeBuilder myTypeBuilder = myModBuilder.DefineType("JumpTableDemo" , TypeAttributes.Public); MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("SwitchMe" , MethodAttributes.Public | MethodAttributes.Static, String.class.ToType(),new Type[] { int.class.ToType() }); ILGenerator myIL = myMthdBuilder.GetILGenerator(); Label defaultCase = myIL.DefineLabel(); Label endOfMethod = myIL.DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. Label jumpTable[] = new Label[] { myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel() }; // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the // correspondence between the possible case values and each index // of the offsets must be established outside of the ILGenerator. // Emit calls, much as a compiler would. myIL.Emit(OpCodes.Ldarg_0); myIL.Emit(OpCodes.Switch, jumpTable); // Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase); // Case arg0 = 0 myIL.MarkLabel(jumpTable[0]); myIL.Emit(OpCodes.Ldstr, "are no bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 1 myIL.MarkLabel(jumpTable[1]); myIL.Emit(OpCodes.Ldstr, "is one banana"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 2 myIL.MarkLabel(jumpTable[2]); myIL.Emit(OpCodes.Ldstr, "are two bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 3 myIL.MarkLabel(jumpTable[3]); myIL.Emit(OpCodes.Ldstr, "are three bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 4 myIL.MarkLabel(jumpTable[4]); myIL.Emit(OpCodes.Ldstr, "are four bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Default case myIL.MarkLabel(defaultCase); myIL.Emit(OpCodes.Ldstr, "are many bananas"); myIL.MarkLabel(endOfMethod); myIL.Emit(OpCodes.Ret); return myTypeBuilder.CreateType(); } //BuildMyType public static void main(String[] args) { Type myType = BuildMyType(); Console.Write("Enter an integer between 0 and 5: "); int theValue = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("---"); Object myInstance = Activator.CreateInstance(myType,new Object[0]); Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe", BindingFlags.InvokeMethod, null, myInstance, new Object[]{(Int32)(theValue)})); } //main } //DynamicJumpTableDemo

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode)
アセンブリ: mscorlib (mscorlib.dll 内)

- opcode

オペコードに引数が必要な場合は、呼び出し元で、引数の長さが宣言されたパラメータの長さと一致することを確認する必要があります。確認しない場合、結果は予測できません。たとえば、Emit 命令に 2 バイトのオペランドが必要で、呼び出し元が 4 バイトのオペランドを渡した場合、Runtime は命令ストリームに追加の 2 バイトを提供します。この追加されたバイトは、Nop 命令になります。

Emit を使用し、ILGenerator のインスタンスを通じて MSIL 出力を生成する方法を次のコード例に示します。
Imports System Imports System.Threading Imports System.Reflection Imports System.Reflection.Emit _ Class DynamicJumpTableDemo Public Shared Function BuildMyType() As Type Dim myDomain As AppDomain = Thread.GetDomain() Dim myAsmName As New AssemblyName() myAsmName.Name = "MyDynamicAssembly" Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _ AssemblyBuilderAccess.Run) Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyJumpTableDemo") Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("JumpTableDemo", _ TypeAttributes.Public) Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("SwitchMe", _ MethodAttributes.Public Or MethodAttributes.Static, _ GetType(String), New Type() {GetType(Integer)}) Dim myIL As ILGenerator = myMthdBuilder.GetILGenerator() Dim defaultCase As Label = myIL.DefineLabel() Dim endOfMethod As Label = myIL.DefineLabel() ' We are initializing our jump table. Note that the labels ' will be placed later using the MarkLabel method. Dim jumpTable() As Label = {myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel(), _ myIL.DefineLabel()} ' arg0, the number we passed, is pushed onto the stack. ' In this case, due to the design of the code sample, ' the value pushed onto the stack happens to match the ' index of the label (in IL terms, the index of the offset ' in the jump table). If this is not the case, such as ' when switching based on non-integer values, rules for the correspondence ' between the possible case values and each index of the offsets ' must be established outside of the ILGenerator.Emit calls, ' much as a compiler would. myIL.Emit(OpCodes.Ldarg_0) myIL.Emit(OpCodes.Switch, jumpTable) ' Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase) ' Case arg0 = 0 myIL.MarkLabel(jumpTable(0)) myIL.Emit(OpCodes.Ldstr, "are no bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 1 myIL.MarkLabel(jumpTable(1)) myIL.Emit(OpCodes.Ldstr, "is one banana") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 2 myIL.MarkLabel(jumpTable(2)) myIL.Emit(OpCodes.Ldstr, "are two bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 3 myIL.MarkLabel(jumpTable(3)) myIL.Emit(OpCodes.Ldstr, "are three bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Case arg0 = 4 myIL.MarkLabel(jumpTable(4)) myIL.Emit(OpCodes.Ldstr, "are four bananas") myIL.Emit(OpCodes.Br_S, endOfMethod) ' Default case myIL.MarkLabel(defaultCase) myIL.Emit(OpCodes.Ldstr, "are many bananas") myIL.MarkLabel(endOfMethod) myIL.Emit(OpCodes.Ret) Return myTypeBuilder.CreateType() End Function 'BuildMyType Public Shared Sub Main() Dim myType As Type = BuildMyType() Console.Write("Enter an integer between 0 and 5: ") Dim theValue As Integer = Convert.ToInt32(Console.ReadLine()) Console.WriteLine("---") Dim myInstance As [Object] = Activator.CreateInstance(myType, New Object() {}) Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe", _ BindingFlags.InvokeMethod, Nothing, _ myInstance, New Object() {theValue})) End Sub 'Main End Class 'DynamicJumpTableDemo
using System; using System.Threading; using System.Reflection; using System.Reflection.Emit; class DynamicJumpTableDemo { public static Type BuildMyType() { AppDomain myDomain = Thread.GetDomain(); AssemblyName myAsmName = new AssemblyName(); myAsmName.Name = "MyDynamicAssembly"; AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule( "MyJumpTableDemo"); TypeBuilder myTypeBuilder = myModBuilder.DefineType("JumpTableDemo" , TypeAttributes.Public); MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("SwitchMe", MethodAttributes.Public | MethodAttributes.Static, typeof(string), new Type[] {typeof(int)}); ILGenerator myIL = myMthdBuilder.GetILGenerator(); Label defaultCase = myIL.DefineLabel(); Label endOfMethod = myIL.DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. Label[] jumpTable = new Label[] { myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel() }; // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the correspondence // between the possible case values and each index of the offsets // must be established outside of the ILGenerator.Emit calls, // much as a compiler would. myIL.Emit(OpCodes.Ldarg_0); myIL.Emit(OpCodes.Switch, jumpTable); // Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase); // Case arg0 = 0 myIL.MarkLabel(jumpTable[0]); myIL.Emit(OpCodes.Ldstr, "are no bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 1 myIL.MarkLabel(jumpTable[1]); myIL.Emit(OpCodes.Ldstr, "is one banana"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 2 myIL.MarkLabel(jumpTable[2]); myIL.Emit(OpCodes.Ldstr, "are two bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 3 myIL.MarkLabel(jumpTable[3]); myIL.Emit(OpCodes.Ldstr, "are three bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 4 myIL.MarkLabel(jumpTable[4]); myIL.Emit(OpCodes.Ldstr, "are four bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Default case myIL.MarkLabel(defaultCase); myIL.Emit(OpCodes.Ldstr, "are many bananas"); myIL.MarkLabel(endOfMethod); myIL.Emit(OpCodes.Ret); return myTypeBuilder.CreateType(); } public static void Main() { Type myType = BuildMyType(); Console.Write("Enter an integer between 0 and 5: "); int theValue = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("---"); Object myInstance = Activator.CreateInstance(myType, new object[0]); Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe" , BindingFlags.InvokeMethod, null, myInstance, new object[] {theValue})); } }
using namespace System; using namespace System::Threading; using namespace System::Reflection; using namespace System::Reflection::Emit; Type^ BuildMyType() { AppDomain^ myDomain = Thread::GetDomain(); AssemblyName^ myAsmName = gcnew AssemblyName; myAsmName->Name = "MyDynamicAssembly"; AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::Run ); ModuleBuilder^ myModBuilder = myAsmBuilder->DefineDynamicModule( "MyJumpTableDemo" ); TypeBuilder^ myTypeBuilder = myModBuilder->DefineType( "JumpTableDemo", TypeAttributes::Public ); array<Type^>^temp0 = {int::typeid}; MethodBuilder^ myMthdBuilder = myTypeBuilder->DefineMethod( "SwitchMe", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), String::typeid, temp0 ); ILGenerator^ myIL = myMthdBuilder->GetILGenerator(); Label defaultCase = myIL->DefineLabel(); Label endOfMethod = myIL->DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. array<Label>^jumpTable = gcnew array<Label>(5); jumpTable[ 0 ] = myIL->DefineLabel(); jumpTable[ 1 ] = myIL->DefineLabel(); jumpTable[ 2 ] = myIL->DefineLabel(); jumpTable[ 3 ] = myIL->DefineLabel(); jumpTable[ 4 ] = myIL->DefineLabel(); // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the correspondence // between the possible case values and each index of the offsets // must be established outside of the ILGenerator::Emit calls, // much as a compiler would. myIL->Emit( OpCodes::Ldarg_0 ); myIL->Emit( OpCodes::Switch, jumpTable ); // Branch on default case myIL->Emit( OpCodes::Br_S, defaultCase ); // Case arg0 = 0 myIL->MarkLabel( jumpTable[ 0 ] ); myIL->Emit( OpCodes::Ldstr, "are no bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 1 myIL->MarkLabel( jumpTable[ 1 ] ); myIL->Emit( OpCodes::Ldstr, "is one banana" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 2 myIL->MarkLabel( jumpTable[ 2 ] ); myIL->Emit( OpCodes::Ldstr, "are two bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 3 myIL->MarkLabel( jumpTable[ 3 ] ); myIL->Emit( OpCodes::Ldstr, "are three bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Case arg0 = 4 myIL->MarkLabel( jumpTable[ 4 ] ); myIL->Emit( OpCodes::Ldstr, "are four bananas" ); myIL->Emit( OpCodes::Br_S, endOfMethod ); // Default case myIL->MarkLabel( defaultCase ); myIL->Emit( OpCodes::Ldstr, "are many bananas" ); myIL->MarkLabel( endOfMethod ); myIL->Emit( OpCodes::Ret ); return myTypeBuilder->CreateType(); } int main() { Type^ myType = BuildMyType(); Console::Write( "Enter an integer between 0 and 5: " ); int theValue = Convert::ToInt32( Console::ReadLine() ); Console::WriteLine( "---" ); Object^ myInstance = Activator::CreateInstance( myType, gcnew array<Object^>(0) ); array<Object^>^temp1 = {theValue}; Console::WriteLine( "Yes, there {0} today!", myType->InvokeMember( "SwitchMe", BindingFlags::InvokeMethod, nullptr, myInstance, temp1 ) ); }
import System .* ; import System.Threading .* ; import System.Reflection .* ; import System.Reflection.Emit .* ; class DynamicJumpTableDemo { public static Type BuildMyType() { AppDomain myDomain = System.Threading.Thread.GetDomain(); AssemblyName myAsmName = new AssemblyName(); myAsmName.set_Name("MyDynamicAssembly"); AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly (myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule ("MyJumpTableDemo"); TypeBuilder myTypeBuilder = myModBuilder.DefineType("JumpTableDemo" , TypeAttributes.Public); MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("SwitchMe" , MethodAttributes.Public | MethodAttributes.Static, String.class.ToType(),new Type[] { int.class.ToType() }); ILGenerator myIL = myMthdBuilder.GetILGenerator(); Label defaultCase = myIL.DefineLabel(); Label endOfMethod = myIL.DefineLabel(); // We are initializing our jump table. Note that the labels // will be placed later using the MarkLabel method. Label jumpTable[] = new Label[] { myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel(), myIL.DefineLabel() }; // arg0, the number we passed, is pushed onto the stack. // In this case, due to the design of the code sample, // the value pushed onto the stack happens to match the // index of the label (in IL terms, the index of the offset // in the jump table). If this is not the case, such as // when switching based on non-integer values, rules for the // correspondence between the possible case values and each index // of the offsets must be established outside of the ILGenerator. // Emit calls, much as a compiler would. myIL.Emit(OpCodes.Ldarg_0); myIL.Emit(OpCodes.Switch, jumpTable); // Branch on default case myIL.Emit(OpCodes.Br_S, defaultCase); // Case arg0 = 0 myIL.MarkLabel(jumpTable[0]); myIL.Emit(OpCodes.Ldstr, "are no bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 1 myIL.MarkLabel(jumpTable[1]); myIL.Emit(OpCodes.Ldstr, "is one banana"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 2 myIL.MarkLabel(jumpTable[2]); myIL.Emit(OpCodes.Ldstr, "are two bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 3 myIL.MarkLabel(jumpTable[3]); myIL.Emit(OpCodes.Ldstr, "are three bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Case arg0 = 4 myIL.MarkLabel(jumpTable[4]); myIL.Emit(OpCodes.Ldstr, "are four bananas"); myIL.Emit(OpCodes.Br_S, endOfMethod); // Default case myIL.MarkLabel(defaultCase); myIL.Emit(OpCodes.Ldstr, "are many bananas"); myIL.MarkLabel(endOfMethod); myIL.Emit(OpCodes.Ret); return myTypeBuilder.CreateType(); } //BuildMyType public static void main(String[] args) { Type myType = BuildMyType(); Console.Write("Enter an integer between 0 and 5: "); int theValue = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("---"); Object myInstance = Activator.CreateInstance(myType,new Object[0]); Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe", BindingFlags.InvokeMethod, null, myInstance, new Object[]{(Int32)(theValue)})); } //main } //DynamicJumpTableDemo

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ILGenerator.Emit メソッド (OpCode, Single)
アセンブリ: mscorlib (mscorlib.dll 内)

- opcode


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からILGenerator.Emitを検索する場合は、下記のリンクをクリックしてください。

- ILGenerator.Emitのページへのリンク