ILGenerator.EmitWriteLine メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ILGenerator.EmitWriteLine メソッドの意味・解説 

ILGenerator.EmitWriteLine メソッド (String)

文字列使用して System.Console.WriteLine を呼び出す MSIL生成します

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable Sub EmitWriteLine
 ( _
    value As String _
)
Dim instance As ILGenerator
Dim value As String

instance.EmitWriteLine(value)
public virtual void EmitWriteLine (
    string value
)
public:
virtual void EmitWriteLine (
    String^ value
)
public void EmitWriteLine (
    String value
)
public function EmitWriteLine (
    value : String
)

パラメータ

value

出力する文字列

解説解説

文字列は、既に定義されている必要があります

使用例使用例

EmitWriteLine メソッド使用して動的なメソッド文字列コンソール書き込む方法については、次のコード例参照してください

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

 _

Class EmitWriteLineDemo
   
   
   Public Shared Function
 CreateDynamicType() As Type

      Dim ctorParams() As Type = {GetType(Integer),
 GetType(Integer)}
      
      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New
 AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      
      Dim myAsmBuilder As AssemblyBuilder =
 myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
      
      Dim pointModule As ModuleBuilder = myAsmBuilder.DefineDynamicModule("PointModule",
 "Point.dll")
      
      Dim pointTypeBld As TypeBuilder = pointModule.DefineType("Point",
 _
                                   TypeAttributes.Public)
      
      Dim xField As FieldBuilder = pointTypeBld.DefineField("x",
 _
                                GetType(Integer),
 _
                                FieldAttributes.Public)
      Dim yField As FieldBuilder = pointTypeBld.DefineField("y",
 _
                                GetType(Integer),
 _
                                 FieldAttributes.Public)
      
      
      Dim objType As Type = Type.GetType("System.Object")
      Dim objCtor As ConstructorInfo = objType.GetConstructor(New
 Type(){})
      
      Dim pointCtor As ConstructorBuilder =
 pointTypeBld.DefineConstructor( _
                             MethodAttributes.Public, _
                             CallingConventions.Standard, _
                             ctorParams)
      Dim ctorIL As ILGenerator = pointCtor.GetILGenerator()
      
      
      ' First, you build the constructor.

      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Call, objCtor)
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_1)
      ctorIL.Emit(OpCodes.Stfld, xField)
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_2)
      ctorIL.Emit(OpCodes.Stfld, yField)
      ctorIL.Emit(OpCodes.Ret)
      
      '  Now, you'll build a method to output some information on the
      ' inside your dynamic class. This method will have the following
      ' definition in C#:
      '  Public Sub WritePoint() 

      Dim writeStrMthd As MethodBuilder = pointTypeBld.DefineMethod("WritePoint",
 _
                                    MethodAttributes.Public, _
                                    Nothing, Nothing)
      
      Dim writeStrIL As ILGenerator = writeStrMthd.GetILGenerator()
      
      ' The below ILGenerator created demonstrates a few ways to create
      ' string output through STDIN. 
      ' ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
      ' call to WriteLine for you.

      writeStrIL.EmitWriteLine("The value of this current instance
 is:")
      
      ' Here, you will do the hard work yourself. First, you need to
 create
      ' the string we will be passing and obtain the correct WriteLine
 overload
      ' for said string. In the below case, you are substituting in
 two values,
      ' so the chosen overload is Console.WriteLine(string, object,
 object).

      Dim inStr As [String] = "({0},
 {1})"
      Dim wlParams() As Type = {GetType(String),
 GetType(Object), GetType(Object)}
      
      ' We need the MethodInfo to pass into EmitCall later.

      Dim writeLineMI As MethodInfo = GetType(Console).GetMethod("WriteLine",
 wlParams)
      
      ' Push the string with the substitutions onto the stack.
      ' This is the first argument for WriteLine - the string one. 

      writeStrIL.Emit(OpCodes.Ldstr, inStr)
      
      ' Since the second argument is an object, and it corresponds to
      ' to the substitution for the value of our integer field, you
 
      ' need to box that field to an object. First, push a reference
      ' to the current instance, and then push the value stored in
      ' field 'x'. We need the reference to the current instance (stored
      ' in local argument index 0) so Ldfld can load from the correct
      ' instance (this one).

      writeStrIL.Emit(OpCodes.Ldarg_0)
      writeStrIL.Emit(OpCodes.Ldfld, xField)
      
      ' Now, we execute the box opcode, which pops the value of field
 'x',
      ' returning a reference to the integer value boxed as an object.

      writeStrIL.Emit(OpCodes.Box, GetType(Integer))
      
      ' Atop the stack, you'll find our string inStr, followed by a
 reference
      ' to the boxed value of 'x'. Now, you need to likewise box field
 'y'.

      writeStrIL.Emit(OpCodes.Ldarg_0)
      writeStrIL.Emit(OpCodes.Ldfld, yField)
      writeStrIL.Emit(OpCodes.Box, GetType(Integer))
      
      ' Now, you have all of the arguments for your call to
      ' Console.WriteLine(string, object, object) atop the stack:
      ' the string InStr, a reference to the boxed value of 'x', and
      ' a reference to the boxed value of 'y'.
      ' Call Console.WriteLine(string, object, object) with EmitCall.

      writeStrIL.EmitCall(OpCodes.Call, writeLineMI, Nothing)
      
      ' Lastly, EmitWriteLine can also output the value of a field
      ' using the overload EmitWriteLine(FieldInfo).

      writeStrIL.EmitWriteLine("The value of 'x'
 is:")
      writeStrIL.EmitWriteLine(xField)
      writeStrIL.EmitWriteLine("The value of 'y'
 is:")
      writeStrIL.EmitWriteLine(yField)
      
      ' Since we return no value (void), the the ret opcode will not
      ' return the top stack value.

      writeStrIL.Emit(OpCodes.Ret)
      
      Return pointTypeBld.CreateType()

   End Function 'CreateDynamicType
    
   
   Public Shared Sub Main()
      
      Dim ctorParams(1) As Object
      
      Console.Write("Enter a integer value for X: ")
      Dim myX As String
 = Console.ReadLine()
      Console.Write("Enter a integer value for Y: ")
      Dim myY As String
 = Console.ReadLine()
      
      Console.WriteLine("---")
      
      ctorParams(0) = Convert.ToInt32(myX)
      ctorParams(1) = Convert.ToInt32(myY)
      
      Dim ptType As Type = CreateDynamicType()

      Dim ptInstance As Object
 = Activator.CreateInstance(ptType, ctorParams)

      ptType.InvokeMember("WritePoint", _
              BindingFlags.InvokeMethod, _
              Nothing, ptInstance, Nothing)

   End Sub 'Main

End Class 'EmitWriteLineDemo

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

class EmitWriteLineDemo {

   public static Type CreateDynamicType() {
       
       Type[] ctorParams = new Type[] {typeof(int)
,
                   typeof(int)};
     
       AppDomain myDomain = Thread.GetDomain();
       AssemblyName myAsmName = new AssemblyName();
       myAsmName.Name = "MyDynamicAssembly";

       AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
                      myAsmName, 
                      AssemblyBuilderAccess.RunAndSave);

       ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule("PointModule"
,
                                    "Point.dll");

       TypeBuilder pointTypeBld = pointModule.DefineType("Point",
                                  TypeAttributes.Public);

       FieldBuilder xField = pointTypeBld.DefineField("x", typeof(int)
,
                                                      FieldAttributes.Public);
       FieldBuilder yField = pointTypeBld.DefineField("y", typeof(int),
 
                                                      FieldAttributes.Public);


       Type objType = Type.GetType("System.Object"); 
       ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);

       ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor(
                                    MethodAttributes.Public,
                                   CallingConventions.Standard,
                                   ctorParams);
       ILGenerator ctorIL = pointCtor.GetILGenerator();


       // First, you build the constructor.
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Call, objCtor);
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Ldarg_1);
       ctorIL.Emit(OpCodes.Stfld, xField); 
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Ldarg_2);
       ctorIL.Emit(OpCodes.Stfld, yField); 
       ctorIL.Emit(OpCodes.Ret); 

       //  Now, you'll build a method to output some information on
 the
       // inside your dynamic class. This method will have the following
       // definition in C#:
    //  public void WritePoint()
      
       MethodBuilder writeStrMthd = pointTypeBld.DefineMethod(
                                     "WritePoint", 
                             MethodAttributes.Public,
                                             typeof(void), 
                                             null);

       
       ILGenerator writeStrIL = writeStrMthd.GetILGenerator();
      
       // The below ILGenerator created demonstrates a few ways to create
       // string output through STDIN. 

       // ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
       // call to WriteLine for you.

       writeStrIL.EmitWriteLine("The value of this current
 instance is:");

       // Here, you will do the hard work yourself. First, you need
 to create
       // the string we will be passing and obtain the correct WriteLine
 overload
       // for said string. In the below case, you are substituting in
 two values,
       // so the chosen overload is Console.WriteLine(string, object,
 object).

       String inStr = "({0}, {1})";
       Type[] wlParams = new Type[] {typeof(string)
,
                     typeof(object),
                     typeof(object)};

       // We need the MethodInfo to pass into EmitCall later.

       MethodInfo writeLineMI = typeof(Console).GetMethod(
                            "WriteLine",
                        wlParams);

       // Push the string with the substitutions onto the stack.
       // This is the first argument for WriteLine - the string one.
 

       writeStrIL.Emit(OpCodes.Ldstr, inStr);

       // Since the second argument is an object, and it corresponds
 to
       // to the substitution for the value of our integer field, you
 
       // need to box that field to an object. First, push a reference
       // to the current instance, and then push the value stored in
       // field 'x'. We need the reference to the current instance (stored
       // in local argument index 0) so Ldfld can load from the correct
       // instance (this one).

       writeStrIL.Emit(OpCodes.Ldarg_0);
       writeStrIL.Emit(OpCodes.Ldfld, xField);

       // Now, we execute the box opcode, which pops the value of field
 'x',
       // returning a reference to the integer value boxed as an object.

       writeStrIL.Emit(OpCodes.Box, typeof(int));

       // Atop the stack, you'll find our string inStr, followed by
 a reference
       // to the boxed value of 'x'. Now, you need to likewise box field
 'y'.

       writeStrIL.Emit(OpCodes.Ldarg_0);
       writeStrIL.Emit(OpCodes.Ldfld, yField);
       writeStrIL.Emit(OpCodes.Box, typeof(int));

       // Now, you have all of the arguments for your call to
       // Console.WriteLine(string, object, object) atop the stack:
       // the string InStr, a reference to the boxed value of 'x', and
       // a reference to the boxed value of 'y'.

       // Call Console.WriteLine(string, object, object) with EmitCall.

       writeStrIL.EmitCall(OpCodes.Call, writeLineMI, null);

       // Lastly, EmitWriteLine can also output the value of a field
       // using the overload EmitWriteLine(FieldInfo).

       writeStrIL.EmitWriteLine("The value of 'x' is:");
       writeStrIL.EmitWriteLine(xField);
       writeStrIL.EmitWriteLine("The value of 'y' is:");
       writeStrIL.EmitWriteLine(yField);

       // Since we return no value (void), the the ret opcode will not
       // return the top stack value.

       writeStrIL.Emit(OpCodes.Ret);
      
       return pointTypeBld.CreateType();

   }

   public static void Main()
 {

      object[] ctorParams = new object[2];

      Console.Write("Enter a integer value for X: ");
 
      string myX = Console.ReadLine();
      Console.Write("Enter a integer value for Y: ");
 
      string myY = Console.ReadLine();

      Console.WriteLine("---");

      ctorParams[0] = Convert.ToInt32(myX);
      ctorParams[1] = Convert.ToInt32(myY);

      Type ptType = CreateDynamicType();
  
      object ptInstance = Activator.CreateInstance(ptType, ctorParams);
      ptType.InvokeMember("WritePoint",
              BindingFlags.InvokeMethod,
              null,
              ptInstance,
              new object[0]);
   }
}

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ CreateDynamicType()
{
   array<Type^>^ctorParams = {int::typeid,int::typeid};
   AppDomain^ myDomain = Thread::GetDomain();
   AssemblyName^ myAsmName = gcnew AssemblyName;
   myAsmName->Name = "MyDynamicAssembly";
   AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName,
 AssemblyBuilderAccess::RunAndSave );
   ModuleBuilder^ pointModule = myAsmBuilder->DefineDynamicModule( "PointModule",
 "Point.dll" );
   TypeBuilder^ pointTypeBld = pointModule->DefineType( "Point", TypeAttributes::Public
 );
   FieldBuilder^ xField = pointTypeBld->DefineField( "x", int::typeid,
 FieldAttributes::Public );
   FieldBuilder^ yField = pointTypeBld->DefineField( "y", int::typeid,
 FieldAttributes::Public );
   Type^ objType = Type::GetType( "System.Object" );
   ConstructorInfo^ objCtor = objType->GetConstructor( gcnew array<Type^>(0)
 );
   ConstructorBuilder^ pointCtor = pointTypeBld->DefineConstructor( MethodAttributes::Public,
 CallingConventions::Standard, ctorParams );
   ILGenerator^ ctorIL = pointCtor->GetILGenerator();
   
   // First, you build the constructor.
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Call, objCtor );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_1 );
   ctorIL->Emit( OpCodes::Stfld, xField );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_2 );
   ctorIL->Emit( OpCodes::Stfld, yField );
   ctorIL->Emit( OpCodes::Ret );
   
   //  Now, you'll build a method to output some information on the
   // inside your dynamic class. This method will have the following
   // definition in C#:
   //  public void WritePoint()
   MethodBuilder^ writeStrMthd = pointTypeBld->DefineMethod( "WritePoint",
 MethodAttributes::Public, void::typeid, nullptr );
   ILGenerator^ writeStrIL = writeStrMthd->GetILGenerator();
   
   // The below ILGenerator created demonstrates a few ways to create
   // String* output through STDIN.
   // ILGenerator::EmitWriteLine(String*) will generate a ldstr and
 a
   // call to WriteLine for you.
   writeStrIL->EmitWriteLine( "The value of this current
 instance is:" );
   
   // Here, you will do the hard work yourself. First, you need to create
   // the String* we will be passing and obtain the correct WriteLine
 overload
   // for said String*. In the below case, you are substituting in two
 values,
   // so the chosen overload is Console::WriteLine(String*, Object*,
 Object*).
   String^ inStr = "( {0}, {1})";
   array<Type^>^wlParams = {String::typeid,Object::typeid,Object::typeid};
   
   // We need the MethodInfo to pass into EmitCall later.
   MethodInfo^ writeLineMI = Console::typeid->GetMethod( "WriteLine",
 wlParams );
   
   // Push the String* with the substitutions onto the stack.
   // This is the first argument for WriteLine - the String* one.
   writeStrIL->Emit( OpCodes::Ldstr, inStr );
   
   // Since the second argument is an Object*, and it corresponds to
   // to the substitution for the value of our integer field, you
   // need to box that field to an Object*. First, push a reference
   // to the current instance, and then push the value stored in
   // field 'x'. We need the reference to the current instance (stored
   // in local argument index 0) so Ldfld can load from the correct
   // instance (this one).
   writeStrIL->Emit( OpCodes::Ldarg_0 );
   writeStrIL->Emit( OpCodes::Ldfld, xField );
   
   // Now, we execute the box opcode, which pops the value of field
 'x',
   // returning a reference to the integer value boxed as an Object*.
   writeStrIL->Emit( OpCodes::Box, int::typeid );
   
   // Atop the stack, you'll find our String* inStr, followed by a reference
   // to the boxed value of 'x'. Now, you need to likewise box field
 'y'.
   writeStrIL->Emit( OpCodes::Ldarg_0 );
   writeStrIL->Emit( OpCodes::Ldfld, yField );
   writeStrIL->Emit( OpCodes::Box, int::typeid );
   
   // Now, you have all of the arguments for your call to
   // Console::WriteLine(String*, Object*, Object*) atop the stack:
   // the String* InStr, a reference to the boxed value of 'x', and
   // a reference to the boxed value of 'y'.
   // Call Console::WriteLine(String*, Object*, Object*) with EmitCall.
   writeStrIL->EmitCall( OpCodes::Call, writeLineMI, nullptr );
   
   // Lastly, EmitWriteLine can also output the value of a field
   // using the overload EmitWriteLine(FieldInfo).
   writeStrIL->EmitWriteLine( "The value of 'x' is:" );
   writeStrIL->EmitWriteLine( xField );
   writeStrIL->EmitWriteLine( "The value of 'y' is:" );
   writeStrIL->EmitWriteLine( yField );
   
   // Since we return no value (void), the the ret opcode will not
   // return the top stack value.
   writeStrIL->Emit( OpCodes::Ret );
   return pointTypeBld->CreateType();
}

int main()
{
   array<Object^>^ctorParams = gcnew array<Object^>(2);
   Console::Write( "Enter a integer value for X: " );
   String^ myX = Console::ReadLine();
   Console::Write( "Enter a integer value for Y: " );
   String^ myY = Console::ReadLine();
   Console::WriteLine( "---" );
   ctorParams[ 0 ] = Convert::ToInt32( myX );
   ctorParams[ 1 ] = Convert::ToInt32( myY );
   Type^ ptType = CreateDynamicType();
   Object^ ptInstance = Activator::CreateInstance( ptType, ctorParams );
   ptType->InvokeMember( "WritePoint", BindingFlags::InvokeMethod, nullptr,
 ptInstance, gcnew array<Object^>(0) );
}

import System.*;
import System.Threading.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

class EmitWriteLineDemo
{
    public static Type CreateDynamicType()
    {
        Type ctorParams[] = new Type[] { int.class.ToType(),
 
            int.class.ToType() };
        AppDomain myDomain = System.Threading.Thread.GetDomain();
        AssemblyName myAsmName = new AssemblyName();
        myAsmName.set_Name("MyDynamicAssembly");
        AssemblyBuilder myAsmBuilder = 
            myDomain.DefineDynamicAssembly(myAsmName,
            AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule(
            "PointModule", "Point.dll");
        TypeBuilder pointTypeBld = pointModule.DefineType("Point",
            TypeAttributes.Public);
        FieldBuilder xField = pointTypeBld.DefineField("x", int.class.ToType()
,
            FieldAttributes.Public);
        FieldBuilder yField = pointTypeBld.DefineField("y", int.class.ToType()
,
            FieldAttributes.Public);
        Type objType = Type.GetType("System.Object");
        ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);
        ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor
            (MethodAttributes.Public, CallingConventions.Standard, ctorParams);
        ILGenerator ctorIL = pointCtor.GetILGenerator();

        // First, you build the constructor.
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Call, objCtor);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_1);
        ctorIL.Emit(OpCodes.Stfld, xField);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_2);
        ctorIL.Emit(OpCodes.Stfld, yField);
        ctorIL.Emit(OpCodes.Ret);

        //  Now, you'll build a method to output some information on
 the
        // inside your dynamic class. This method will have the following
        // definition in VJ#:
        //  public void WritePoint()
        MethodBuilder writeStrMthd = pointTypeBld.DefineMethod("WritePoint"
,
            MethodAttributes.Public, void.class.ToType(),
 null);
        ILGenerator writeStrIL = writeStrMthd.GetILGenerator();

        // The below ILGenerator created demonstrates a few ways to
 create
        // string output through STDIN. 
        // ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
        // call to WriteLine for you.
        writeStrIL.EmitWriteLine("The value of this current
 instance is:");

        // Here, you will do the hard work yourself. First, you need
 to create
        // the string we will be passing and obtain the correct WriteLine
        // overload for said string. In the below case, you are substituting
        // in two values, so the chosen overload is
        // Console.WriteLine(string, object, object).
        String inStr = "({0}, {1})";
        Type wlParams[] = new Type[] { String.class.ToType()
,
            Object.class.ToType(), Object.class.ToType()
 };

        // We need the MethodInfo to pass into EmitCall later.
        MethodInfo writeLineMI = Console.class.ToType().GetMethod("WriteLine"
,
            wlParams);

        // Push the string with the substitutions onto the stack.
        // This is the first argument for WriteLine - the string one.
 
        writeStrIL.Emit(OpCodes.Ldstr, inStr);

        // Since the second argument is an object, and it corresponds
 to
        // to the substitution for the value of our integer field, you
 
        // need to box that field to an object. First, push a reference
        // to the current instance, and then push the value stored in
        // field 'x'. We need the reference to the current instance
 (stored
        // in local argument index 0) so Ldfld can load from the correct
        // instance (this one).
        writeStrIL.Emit(OpCodes.Ldarg_0);
        writeStrIL.Emit(OpCodes.Ldfld, xField);

        // Now, we execute the box opcode, which pops the value of field
 'x',
        // returning a reference to the integer value boxed as an object.
        writeStrIL.Emit(OpCodes.Box, int.class.ToType());

        // Atop the stack, you'll find our string inStr, followed by
 a 
        // reference to the boxed value of 'x'. Now, you need to likewise
        // box field 'y'.
        writeStrIL.Emit(OpCodes.Ldarg_0);
        writeStrIL.Emit(OpCodes.Ldfld, yField);
        writeStrIL.Emit(OpCodes.Box, int.class.ToType());

        // Now, you have all of the arguments for your call to
        // Console.WriteLine(string, object, object) atop the stack:
        // the string InStr, a reference to the boxed value of 'x',
 and
        // a reference to the boxed value of 'y'.
        // Call Console.WriteLine(string, object, object) with EmitCall.
        writeStrIL.EmitCall(OpCodes.Call, writeLineMI, null);

        // Lastly, EmitWriteLine can also output the value of a field
        // using the overload EmitWriteLine(FieldInfo).
        writeStrIL.EmitWriteLine("The value of 'x' is:");
        writeStrIL.EmitWriteLine(xField);
        writeStrIL.EmitWriteLine("The value of 'y' is:");
        writeStrIL.EmitWriteLine(yField);

        // Since we return no value (void), the the ret opcode will
 not
        // return the top stack value.
        writeStrIL.Emit(OpCodes.Ret);
        return pointTypeBld.CreateType();
    } //CreateDynamicType
    
    public static void main(String[]
 args)
    {
        Object ctorParams[] = new Object[2];
        Console.Write("Enter a integer value for X: ");
        String myX = Console.ReadLine();
        Console.Write("Enter a integer value for Y: ");
        String myY = Console.ReadLine();
        Console.WriteLine("---");
        ctorParams[0] = (Int32)Integer.parseInt(myX);
        ctorParams[1] = (Int32)Integer.parseInt(myY);
        Type ptType = CreateDynamicType();
        Object ptInstance = Activator.CreateInstance(ptType, ctorParams);
        ptType.InvokeMember("WritePoint", BindingFlags.InvokeMethod, null
,
            ptInstance, new Object[0]);
    } //main
} //EmitWriteLineDemo
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ILGenerator クラス
ILGenerator メンバ
System.Reflection.Emit 名前空間

ILGenerator.EmitWriteLine メソッド (LocalBuilder)

指定したローカル変数使用して System.Console.WriteLine を呼び出すために必要な MSIL生成します

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable Sub EmitWriteLine
 ( _
    localBuilder As LocalBuilder _
)
Dim instance As ILGenerator
Dim localBuilder As LocalBuilder

instance.EmitWriteLine(localBuilder)
public virtual void EmitWriteLine (
    LocalBuilder localBuilder
)
public:
virtual void EmitWriteLine (
    LocalBuilder^ localBuilder
)
public void EmitWriteLine (
    LocalBuilder localBuilder
)
public function EmitWriteLine (
    localBuilder : LocalBuilder
)

パラメータ

localBuilder

値がコンソール書き込まれローカル変数

例外例外
例外種類条件

ArgumentException

localBuilder の型が、サポートされていない TypeBuilder または EnumBuilder です。

または

localBuilder の型を受け取System.Console.WriteLineオーバーロードがありません。

ArgumentNullException

localBuildernull 参照 (Visual Basic では Nothing) です。

解説解説

localBuilder の型は、System.Console.WriteLine メソッドオーバーロードパラメータ型と一致している必要があります

使用例使用例

EmitWriteLine メソッド使用して動的なメソッド文字列コンソール書き込む方法については、次のコード例参照してください

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

 _

Class EmitWriteLineDemo
   
   
   Public Shared Function
 CreateDynamicType() As Type

      Dim ctorParams() As Type = {GetType(Integer),
 GetType(Integer)}
      
      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New
 AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      
      Dim myAsmBuilder As AssemblyBuilder =
 myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
      
      Dim pointModule As ModuleBuilder = myAsmBuilder.DefineDynamicModule("PointModule",
 "Point.dll")
      
      Dim pointTypeBld As TypeBuilder = pointModule.DefineType("Point",
 _
                                   TypeAttributes.Public)
      
      Dim xField As FieldBuilder = pointTypeBld.DefineField("x",
 _
                                GetType(Integer),
 _
                                FieldAttributes.Public)
      Dim yField As FieldBuilder = pointTypeBld.DefineField("y",
 _
                                GetType(Integer),
 _
                                 FieldAttributes.Public)
      
      
      Dim objType As Type = Type.GetType("System.Object")
      Dim objCtor As ConstructorInfo = objType.GetConstructor(New
 Type(){})
      
      Dim pointCtor As ConstructorBuilder =
 pointTypeBld.DefineConstructor( _
                             MethodAttributes.Public, _
                             CallingConventions.Standard, _
                             ctorParams)
      Dim ctorIL As ILGenerator = pointCtor.GetILGenerator()
      
      
      ' First, you build the constructor.

      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Call, objCtor)
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_1)
      ctorIL.Emit(OpCodes.Stfld, xField)
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_2)
      ctorIL.Emit(OpCodes.Stfld, yField)
      ctorIL.Emit(OpCodes.Ret)
      
      '  Now, you'll build a method to output some information on the
      ' inside your dynamic class. This method will have the following
      ' definition in C#:
      '  Public Sub WritePoint() 

      Dim writeStrMthd As MethodBuilder = pointTypeBld.DefineMethod("WritePoint",
 _
                                    MethodAttributes.Public, _
                                    Nothing, Nothing)
      
      Dim writeStrIL As ILGenerator = writeStrMthd.GetILGenerator()
      
      ' The below ILGenerator created demonstrates a few ways to create
      ' string output through STDIN. 
      ' ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
      ' call to WriteLine for you.

      writeStrIL.EmitWriteLine("The value of this current instance
 is:")
      
      ' Here, you will do the hard work yourself. First, you need to
 create
      ' the string we will be passing and obtain the correct WriteLine
 overload
      ' for said string. In the below case, you are substituting in
 two values,
      ' so the chosen overload is Console.WriteLine(string, object,
 object).

      Dim inStr As [String] = "({0},
 {1})"
      Dim wlParams() As Type = {GetType(String),
 GetType(Object), GetType(Object)}
      
      ' We need the MethodInfo to pass into EmitCall later.

      Dim writeLineMI As MethodInfo = GetType(Console).GetMethod("WriteLine",
 wlParams)
      
      ' Push the string with the substitutions onto the stack.
      ' This is the first argument for WriteLine - the string one. 

      writeStrIL.Emit(OpCodes.Ldstr, inStr)
      
      ' Since the second argument is an object, and it corresponds to
      ' to the substitution for the value of our integer field, you
 
      ' need to box that field to an object. First, push a reference
      ' to the current instance, and then push the value stored in
      ' field 'x'. We need the reference to the current instance (stored
      ' in local argument index 0) so Ldfld can load from the correct
      ' instance (this one).

      writeStrIL.Emit(OpCodes.Ldarg_0)
      writeStrIL.Emit(OpCodes.Ldfld, xField)
      
      ' Now, we execute the box opcode, which pops the value of field
 'x',
      ' returning a reference to the integer value boxed as an object.

      writeStrIL.Emit(OpCodes.Box, GetType(Integer))
      
      ' Atop the stack, you'll find our string inStr, followed by a
 reference
      ' to the boxed value of 'x'. Now, you need to likewise box field
 'y'.

      writeStrIL.Emit(OpCodes.Ldarg_0)
      writeStrIL.Emit(OpCodes.Ldfld, yField)
      writeStrIL.Emit(OpCodes.Box, GetType(Integer))
      
      ' Now, you have all of the arguments for your call to
      ' Console.WriteLine(string, object, object) atop the stack:
      ' the string InStr, a reference to the boxed value of 'x', and
      ' a reference to the boxed value of 'y'.
      ' Call Console.WriteLine(string, object, object) with EmitCall.

      writeStrIL.EmitCall(OpCodes.Call, writeLineMI, Nothing)
      
      ' Lastly, EmitWriteLine can also output the value of a field
      ' using the overload EmitWriteLine(FieldInfo).

      writeStrIL.EmitWriteLine("The value of 'x'
 is:")
      writeStrIL.EmitWriteLine(xField)
      writeStrIL.EmitWriteLine("The value of 'y'
 is:")
      writeStrIL.EmitWriteLine(yField)
      
      ' Since we return no value (void), the the ret opcode will not
      ' return the top stack value.

      writeStrIL.Emit(OpCodes.Ret)
      
      Return pointTypeBld.CreateType()

   End Function 'CreateDynamicType
    
   
   Public Shared Sub Main()
      
      Dim ctorParams(1) As Object
      
      Console.Write("Enter a integer value for X: ")
      Dim myX As String
 = Console.ReadLine()
      Console.Write("Enter a integer value for Y: ")
      Dim myY As String
 = Console.ReadLine()
      
      Console.WriteLine("---")
      
      ctorParams(0) = Convert.ToInt32(myX)
      ctorParams(1) = Convert.ToInt32(myY)
      
      Dim ptType As Type = CreateDynamicType()

      Dim ptInstance As Object
 = Activator.CreateInstance(ptType, ctorParams)

      ptType.InvokeMember("WritePoint", _
              BindingFlags.InvokeMethod, _
              Nothing, ptInstance, Nothing)

   End Sub 'Main

End Class 'EmitWriteLineDemo

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

class EmitWriteLineDemo {

   public static Type CreateDynamicType() {
       
       Type[] ctorParams = new Type[] {typeof(int)
,
                   typeof(int)};
     
       AppDomain myDomain = Thread.GetDomain();
       AssemblyName myAsmName = new AssemblyName();
       myAsmName.Name = "MyDynamicAssembly";

       AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
                      myAsmName, 
                      AssemblyBuilderAccess.RunAndSave);

       ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule("PointModule"
,
                                    "Point.dll");

       TypeBuilder pointTypeBld = pointModule.DefineType("Point",
                                  TypeAttributes.Public);

       FieldBuilder xField = pointTypeBld.DefineField("x", typeof(int)
,
                                                      FieldAttributes.Public);
       FieldBuilder yField = pointTypeBld.DefineField("y", typeof(int),
 
                                                      FieldAttributes.Public);


       Type objType = Type.GetType("System.Object"); 
       ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);

       ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor(
                                    MethodAttributes.Public,
                                   CallingConventions.Standard,
                                   ctorParams);
       ILGenerator ctorIL = pointCtor.GetILGenerator();


       // First, you build the constructor.
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Call, objCtor);
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Ldarg_1);
       ctorIL.Emit(OpCodes.Stfld, xField); 
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Ldarg_2);
       ctorIL.Emit(OpCodes.Stfld, yField); 
       ctorIL.Emit(OpCodes.Ret); 

       //  Now, you'll build a method to output some information on
 the
       // inside your dynamic class. This method will have the following
       // definition in C#:
    //  public void WritePoint()
      
       MethodBuilder writeStrMthd = pointTypeBld.DefineMethod(
                                     "WritePoint", 
                             MethodAttributes.Public,
                                             typeof(void), 
                                             null);

       
       ILGenerator writeStrIL = writeStrMthd.GetILGenerator();
      
       // The below ILGenerator created demonstrates a few ways to create
       // string output through STDIN. 

       // ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
       // call to WriteLine for you.

       writeStrIL.EmitWriteLine("The value of this current
 instance is:");

       // Here, you will do the hard work yourself. First, you need
 to create
       // the string we will be passing and obtain the correct WriteLine
 overload
       // for said string. In the below case, you are substituting in
 two values,
       // so the chosen overload is Console.WriteLine(string, object,
 object).

       String inStr = "({0}, {1})";
       Type[] wlParams = new Type[] {typeof(string)
,
                     typeof(object),
                     typeof(object)};

       // We need the MethodInfo to pass into EmitCall later.

       MethodInfo writeLineMI = typeof(Console).GetMethod(
                            "WriteLine",
                        wlParams);

       // Push the string with the substitutions onto the stack.
       // This is the first argument for WriteLine - the string one.
 

       writeStrIL.Emit(OpCodes.Ldstr, inStr);

       // Since the second argument is an object, and it corresponds
 to
       // to the substitution for the value of our integer field, you
 
       // need to box that field to an object. First, push a reference
       // to the current instance, and then push the value stored in
       // field 'x'. We need the reference to the current instance (stored
       // in local argument index 0) so Ldfld can load from the correct
       // instance (this one).

       writeStrIL.Emit(OpCodes.Ldarg_0);
       writeStrIL.Emit(OpCodes.Ldfld, xField);

       // Now, we execute the box opcode, which pops the value of field
 'x',
       // returning a reference to the integer value boxed as an object.

       writeStrIL.Emit(OpCodes.Box, typeof(int));

       // Atop the stack, you'll find our string inStr, followed by
 a reference
       // to the boxed value of 'x'. Now, you need to likewise box field
 'y'.

       writeStrIL.Emit(OpCodes.Ldarg_0);
       writeStrIL.Emit(OpCodes.Ldfld, yField);
       writeStrIL.Emit(OpCodes.Box, typeof(int));

       // Now, you have all of the arguments for your call to
       // Console.WriteLine(string, object, object) atop the stack:
       // the string InStr, a reference to the boxed value of 'x', and
       // a reference to the boxed value of 'y'.

       // Call Console.WriteLine(string, object, object) with EmitCall.

       writeStrIL.EmitCall(OpCodes.Call, writeLineMI, null);

       // Lastly, EmitWriteLine can also output the value of a field
       // using the overload EmitWriteLine(FieldInfo).

       writeStrIL.EmitWriteLine("The value of 'x' is:");
       writeStrIL.EmitWriteLine(xField);
       writeStrIL.EmitWriteLine("The value of 'y' is:");
       writeStrIL.EmitWriteLine(yField);

       // Since we return no value (void), the the ret opcode will not
       // return the top stack value.

       writeStrIL.Emit(OpCodes.Ret);
      
       return pointTypeBld.CreateType();

   }

   public static void Main()
 {

      object[] ctorParams = new object[2];

      Console.Write("Enter a integer value for X: ");
 
      string myX = Console.ReadLine();
      Console.Write("Enter a integer value for Y: ");
 
      string myY = Console.ReadLine();

      Console.WriteLine("---");

      ctorParams[0] = Convert.ToInt32(myX);
      ctorParams[1] = Convert.ToInt32(myY);

      Type ptType = CreateDynamicType();
  
      object ptInstance = Activator.CreateInstance(ptType, ctorParams);
      ptType.InvokeMember("WritePoint",
              BindingFlags.InvokeMethod,
              null,
              ptInstance,
              new object[0]);
   }
}

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ CreateDynamicType()
{
   array<Type^>^ctorParams = {int::typeid,int::typeid};
   AppDomain^ myDomain = Thread::GetDomain();
   AssemblyName^ myAsmName = gcnew AssemblyName;
   myAsmName->Name = "MyDynamicAssembly";
   AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName,
 AssemblyBuilderAccess::RunAndSave );
   ModuleBuilder^ pointModule = myAsmBuilder->DefineDynamicModule( "PointModule",
 "Point.dll" );
   TypeBuilder^ pointTypeBld = pointModule->DefineType( "Point", TypeAttributes::Public
 );
   FieldBuilder^ xField = pointTypeBld->DefineField( "x", int::typeid,
 FieldAttributes::Public );
   FieldBuilder^ yField = pointTypeBld->DefineField( "y", int::typeid,
 FieldAttributes::Public );
   Type^ objType = Type::GetType( "System.Object" );
   ConstructorInfo^ objCtor = objType->GetConstructor( gcnew array<Type^>(0)
 );
   ConstructorBuilder^ pointCtor = pointTypeBld->DefineConstructor( MethodAttributes::Public,
 CallingConventions::Standard, ctorParams );
   ILGenerator^ ctorIL = pointCtor->GetILGenerator();
   
   // First, you build the constructor.
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Call, objCtor );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_1 );
   ctorIL->Emit( OpCodes::Stfld, xField );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_2 );
   ctorIL->Emit( OpCodes::Stfld, yField );
   ctorIL->Emit( OpCodes::Ret );
   
   //  Now, you'll build a method to output some information on the
   // inside your dynamic class. This method will have the following
   // definition in C#:
   //  public void WritePoint()
   MethodBuilder^ writeStrMthd = pointTypeBld->DefineMethod( "WritePoint",
 MethodAttributes::Public, void::typeid, nullptr );
   ILGenerator^ writeStrIL = writeStrMthd->GetILGenerator();
   
   // The below ILGenerator created demonstrates a few ways to create
   // String* output through STDIN.
   // ILGenerator::EmitWriteLine(String*) will generate a ldstr and
 a
   // call to WriteLine for you.
   writeStrIL->EmitWriteLine( "The value of this current
 instance is:" );
   
   // Here, you will do the hard work yourself. First, you need to create
   // the String* we will be passing and obtain the correct WriteLine
 overload
   // for said String*. In the below case, you are substituting in two
 values,
   // so the chosen overload is Console::WriteLine(String*, Object*,
 Object*).
   String^ inStr = "( {0}, {1})";
   array<Type^>^wlParams = {String::typeid,Object::typeid,Object::typeid};
   
   // We need the MethodInfo to pass into EmitCall later.
   MethodInfo^ writeLineMI = Console::typeid->GetMethod( "WriteLine",
 wlParams );
   
   // Push the String* with the substitutions onto the stack.
   // This is the first argument for WriteLine - the String* one.
   writeStrIL->Emit( OpCodes::Ldstr, inStr );
   
   // Since the second argument is an Object*, and it corresponds to
   // to the substitution for the value of our integer field, you
   // need to box that field to an Object*. First, push a reference
   // to the current instance, and then push the value stored in
   // field 'x'. We need the reference to the current instance (stored
   // in local argument index 0) so Ldfld can load from the correct
   // instance (this one).
   writeStrIL->Emit( OpCodes::Ldarg_0 );
   writeStrIL->Emit( OpCodes::Ldfld, xField );
   
   // Now, we execute the box opcode, which pops the value of field
 'x',
   // returning a reference to the integer value boxed as an Object*.
   writeStrIL->Emit( OpCodes::Box, int::typeid );
   
   // Atop the stack, you'll find our String* inStr, followed by a reference
   // to the boxed value of 'x'. Now, you need to likewise box field
 'y'.
   writeStrIL->Emit( OpCodes::Ldarg_0 );
   writeStrIL->Emit( OpCodes::Ldfld, yField );
   writeStrIL->Emit( OpCodes::Box, int::typeid );
   
   // Now, you have all of the arguments for your call to
   // Console::WriteLine(String*, Object*, Object*) atop the stack:
   // the String* InStr, a reference to the boxed value of 'x', and
   // a reference to the boxed value of 'y'.
   // Call Console::WriteLine(String*, Object*, Object*) with EmitCall.
   writeStrIL->EmitCall( OpCodes::Call, writeLineMI, nullptr );
   
   // Lastly, EmitWriteLine can also output the value of a field
   // using the overload EmitWriteLine(FieldInfo).
   writeStrIL->EmitWriteLine( "The value of 'x' is:" );
   writeStrIL->EmitWriteLine( xField );
   writeStrIL->EmitWriteLine( "The value of 'y' is:" );
   writeStrIL->EmitWriteLine( yField );
   
   // Since we return no value (void), the the ret opcode will not
   // return the top stack value.
   writeStrIL->Emit( OpCodes::Ret );
   return pointTypeBld->CreateType();
}

int main()
{
   array<Object^>^ctorParams = gcnew array<Object^>(2);
   Console::Write( "Enter a integer value for X: " );
   String^ myX = Console::ReadLine();
   Console::Write( "Enter a integer value for Y: " );
   String^ myY = Console::ReadLine();
   Console::WriteLine( "---" );
   ctorParams[ 0 ] = Convert::ToInt32( myX );
   ctorParams[ 1 ] = Convert::ToInt32( myY );
   Type^ ptType = CreateDynamicType();
   Object^ ptInstance = Activator::CreateInstance( ptType, ctorParams );
   ptType->InvokeMember( "WritePoint", BindingFlags::InvokeMethod, nullptr,
 ptInstance, gcnew array<Object^>(0) );
}

import System.*;
import System.Threading.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

class EmitWriteLineDemo
{
    public static Type CreateDynamicType()
    {
        Type ctorParams[] = new Type[] { int.class.ToType(),
 
            int.class.ToType() };
        AppDomain myDomain = System.Threading.Thread.GetDomain();
        AssemblyName myAsmName = new AssemblyName();
        myAsmName.set_Name("MyDynamicAssembly");
        AssemblyBuilder myAsmBuilder = 
            myDomain.DefineDynamicAssembly(myAsmName,
            AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule(
            "PointModule", "Point.dll");
        TypeBuilder pointTypeBld = pointModule.DefineType("Point",
            TypeAttributes.Public);
        FieldBuilder xField = pointTypeBld.DefineField("x", int.class.ToType()
,
            FieldAttributes.Public);
        FieldBuilder yField = pointTypeBld.DefineField("y", int.class.ToType()
,
            FieldAttributes.Public);
        Type objType = Type.GetType("System.Object");
        ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);
        ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor
            (MethodAttributes.Public, CallingConventions.Standard, ctorParams);
        ILGenerator ctorIL = pointCtor.GetILGenerator();

        // First, you build the constructor.
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Call, objCtor);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_1);
        ctorIL.Emit(OpCodes.Stfld, xField);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_2);
        ctorIL.Emit(OpCodes.Stfld, yField);
        ctorIL.Emit(OpCodes.Ret);

        //  Now, you'll build a method to output some information on
 the
        // inside your dynamic class. This method will have the following
        // definition in VJ#:
        //  public void WritePoint()
        MethodBuilder writeStrMthd = pointTypeBld.DefineMethod("WritePoint"
,
            MethodAttributes.Public, void.class.ToType(),
 null);
        ILGenerator writeStrIL = writeStrMthd.GetILGenerator();

        // The below ILGenerator created demonstrates a few ways to
 create
        // string output through STDIN. 
        // ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
        // call to WriteLine for you.
        writeStrIL.EmitWriteLine("The value of this current
 instance is:");

        // Here, you will do the hard work yourself. First, you need
 to create
        // the string we will be passing and obtain the correct WriteLine
        // overload for said string. In the below case, you are substituting
        // in two values, so the chosen overload is
        // Console.WriteLine(string, object, object).
        String inStr = "({0}, {1})";
        Type wlParams[] = new Type[] { String.class.ToType()
,
            Object.class.ToType(), Object.class.ToType()
 };

        // We need the MethodInfo to pass into EmitCall later.
        MethodInfo writeLineMI = Console.class.ToType().GetMethod("WriteLine"
,
            wlParams);

        // Push the string with the substitutions onto the stack.
        // This is the first argument for WriteLine - the string one.
 
        writeStrIL.Emit(OpCodes.Ldstr, inStr);

        // Since the second argument is an object, and it corresponds
 to
        // to the substitution for the value of our integer field, you
 
        // need to box that field to an object. First, push a reference
        // to the current instance, and then push the value stored in
        // field 'x'. We need the reference to the current instance
 (stored
        // in local argument index 0) so Ldfld can load from the correct
        // instance (this one).
        writeStrIL.Emit(OpCodes.Ldarg_0);
        writeStrIL.Emit(OpCodes.Ldfld, xField);

        // Now, we execute the box opcode, which pops the value of field
 'x',
        // returning a reference to the integer value boxed as an object.
        writeStrIL.Emit(OpCodes.Box, int.class.ToType());

        // Atop the stack, you'll find our string inStr, followed by
 a 
        // reference to the boxed value of 'x'. Now, you need to likewise
        // box field 'y'.
        writeStrIL.Emit(OpCodes.Ldarg_0);
        writeStrIL.Emit(OpCodes.Ldfld, yField);
        writeStrIL.Emit(OpCodes.Box, int.class.ToType());

        // Now, you have all of the arguments for your call to
        // Console.WriteLine(string, object, object) atop the stack:
        // the string InStr, a reference to the boxed value of 'x',
 and
        // a reference to the boxed value of 'y'.
        // Call Console.WriteLine(string, object, object) with EmitCall.
        writeStrIL.EmitCall(OpCodes.Call, writeLineMI, null);

        // Lastly, EmitWriteLine can also output the value of a field
        // using the overload EmitWriteLine(FieldInfo).
        writeStrIL.EmitWriteLine("The value of 'x' is:");
        writeStrIL.EmitWriteLine(xField);
        writeStrIL.EmitWriteLine("The value of 'y' is:");
        writeStrIL.EmitWriteLine(yField);

        // Since we return no value (void), the the ret opcode will
 not
        // return the top stack value.
        writeStrIL.Emit(OpCodes.Ret);
        return pointTypeBld.CreateType();
    } //CreateDynamicType
    
    public static void main(String[]
 args)
    {
        Object ctorParams[] = new Object[2];
        Console.Write("Enter a integer value for X: ");
        String myX = Console.ReadLine();
        Console.Write("Enter a integer value for Y: ");
        String myY = Console.ReadLine();
        Console.WriteLine("---");
        ctorParams[0] = (Int32)Integer.parseInt(myX);
        ctorParams[1] = (Int32)Integer.parseInt(myY);
        Type ptType = CreateDynamicType();
        Object ptInstance = Activator.CreateInstance(ptType, ctorParams);
        ptType.InvokeMember("WritePoint", BindingFlags.InvokeMethod, null
,
            ptInstance, new Object[0]);
    } //main
} //EmitWriteLineDemo
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ILGenerator クラス
ILGenerator メンバ
System.Reflection.Emit 名前空間

ILGenerator.EmitWriteLine メソッド (FieldInfo)

指定したフィールド使用して System.Console.WriteLine を呼び出すために必要な MSIL生成します

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable Sub EmitWriteLine
 ( _
    fld As FieldInfo _
)
Dim instance As ILGenerator
Dim fld As FieldInfo

instance.EmitWriteLine(fld)
public virtual void EmitWriteLine (
    FieldInfo fld
)
public:
virtual void EmitWriteLine (
    FieldInfo^ fld
)
public void EmitWriteLine (
    FieldInfo fld
)
public function EmitWriteLine (
    fld : FieldInfo
)

パラメータ

fld

値がコンソール書き込まれフィールド

例外例外
例外種類条件

ArgumentException

指定されフィールドの型を受け取System.Console.WriteLine メソッドオーバーロードがありません。

ArgumentNullException

fldnull 参照 (Visual Basic では Nothing) です。

NotSupportedException

フィールドの型が、サポートされていない TypeBuilder または EnumBuilder です。

解説解説

fld の型は、System.Console.WriteLine メソッドオーバーロードパラメータ型と一致している必要があります

使用例使用例

EmitWriteLine メソッド使用して動的なメソッド文字列コンソール書き込む方法については、次のコード例参照してください

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

 _

Class EmitWriteLineDemo
   
   
   Public Shared Function
 CreateDynamicType() As Type

      Dim ctorParams() As Type = {GetType(Integer),
 GetType(Integer)}
      
      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New
 AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      
      Dim myAsmBuilder As AssemblyBuilder =
 myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
      
      Dim pointModule As ModuleBuilder = myAsmBuilder.DefineDynamicModule("PointModule",
 "Point.dll")
      
      Dim pointTypeBld As TypeBuilder = pointModule.DefineType("Point",
 _
                                   TypeAttributes.Public)
      
      Dim xField As FieldBuilder = pointTypeBld.DefineField("x",
 _
                                GetType(Integer),
 _
                                FieldAttributes.Public)
      Dim yField As FieldBuilder = pointTypeBld.DefineField("y",
 _
                                GetType(Integer),
 _
                                 FieldAttributes.Public)
      
      
      Dim objType As Type = Type.GetType("System.Object")
      Dim objCtor As ConstructorInfo = objType.GetConstructor(New
 Type(){})
      
      Dim pointCtor As ConstructorBuilder =
 pointTypeBld.DefineConstructor( _
                             MethodAttributes.Public, _
                             CallingConventions.Standard, _
                             ctorParams)
      Dim ctorIL As ILGenerator = pointCtor.GetILGenerator()
      
      
      ' First, you build the constructor.

      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Call, objCtor)
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_1)
      ctorIL.Emit(OpCodes.Stfld, xField)
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_2)
      ctorIL.Emit(OpCodes.Stfld, yField)
      ctorIL.Emit(OpCodes.Ret)
      
      '  Now, you'll build a method to output some information on the
      ' inside your dynamic class. This method will have the following
      ' definition in C#:
      '  Public Sub WritePoint() 

      Dim writeStrMthd As MethodBuilder = pointTypeBld.DefineMethod("WritePoint",
 _
                                    MethodAttributes.Public, _
                                    Nothing, Nothing)
      
      Dim writeStrIL As ILGenerator = writeStrMthd.GetILGenerator()
      
      ' The below ILGenerator created demonstrates a few ways to create
      ' string output through STDIN. 
      ' ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
      ' call to WriteLine for you.

      writeStrIL.EmitWriteLine("The value of this current instance
 is:")
      
      ' Here, you will do the hard work yourself. First, you need to
 create
      ' the string we will be passing and obtain the correct WriteLine
 overload
      ' for said string. In the below case, you are substituting in
 two values,
      ' so the chosen overload is Console.WriteLine(string, object,
 object).

      Dim inStr As [String] = "({0},
 {1})"
      Dim wlParams() As Type = {GetType(String),
 GetType(Object), GetType(Object)}
      
      ' We need the MethodInfo to pass into EmitCall later.

      Dim writeLineMI As MethodInfo = GetType(Console).GetMethod("WriteLine",
 wlParams)
      
      ' Push the string with the substitutions onto the stack.
      ' This is the first argument for WriteLine - the string one. 

      writeStrIL.Emit(OpCodes.Ldstr, inStr)
      
      ' Since the second argument is an object, and it corresponds to
      ' to the substitution for the value of our integer field, you
 
      ' need to box that field to an object. First, push a reference
      ' to the current instance, and then push the value stored in
      ' field 'x'. We need the reference to the current instance (stored
      ' in local argument index 0) so Ldfld can load from the correct
      ' instance (this one).

      writeStrIL.Emit(OpCodes.Ldarg_0)
      writeStrIL.Emit(OpCodes.Ldfld, xField)
      
      ' Now, we execute the box opcode, which pops the value of field
 'x',
      ' returning a reference to the integer value boxed as an object.

      writeStrIL.Emit(OpCodes.Box, GetType(Integer))
      
      ' Atop the stack, you'll find our string inStr, followed by a
 reference
      ' to the boxed value of 'x'. Now, you need to likewise box field
 'y'.

      writeStrIL.Emit(OpCodes.Ldarg_0)
      writeStrIL.Emit(OpCodes.Ldfld, yField)
      writeStrIL.Emit(OpCodes.Box, GetType(Integer))
      
      ' Now, you have all of the arguments for your call to
      ' Console.WriteLine(string, object, object) atop the stack:
      ' the string InStr, a reference to the boxed value of 'x', and
      ' a reference to the boxed value of 'y'.
      ' Call Console.WriteLine(string, object, object) with EmitCall.

      writeStrIL.EmitCall(OpCodes.Call, writeLineMI, Nothing)
      
      ' Lastly, EmitWriteLine can also output the value of a field
      ' using the overload EmitWriteLine(FieldInfo).

      writeStrIL.EmitWriteLine("The value of 'x'
 is:")
      writeStrIL.EmitWriteLine(xField)
      writeStrIL.EmitWriteLine("The value of 'y'
 is:")
      writeStrIL.EmitWriteLine(yField)
      
      ' Since we return no value (void), the the ret opcode will not
      ' return the top stack value.

      writeStrIL.Emit(OpCodes.Ret)
      
      Return pointTypeBld.CreateType()

   End Function 'CreateDynamicType
    
   
   Public Shared Sub Main()
      
      Dim ctorParams(1) As Object
      
      Console.Write("Enter a integer value for X: ")
      Dim myX As String
 = Console.ReadLine()
      Console.Write("Enter a integer value for Y: ")
      Dim myY As String
 = Console.ReadLine()
      
      Console.WriteLine("---")
      
      ctorParams(0) = Convert.ToInt32(myX)
      ctorParams(1) = Convert.ToInt32(myY)
      
      Dim ptType As Type = CreateDynamicType()

      Dim ptInstance As Object
 = Activator.CreateInstance(ptType, ctorParams)

      ptType.InvokeMember("WritePoint", _
              BindingFlags.InvokeMethod, _
              Nothing, ptInstance, Nothing)

   End Sub 'Main

End Class 'EmitWriteLineDemo

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

class EmitWriteLineDemo {

   public static Type CreateDynamicType() {
       
       Type[] ctorParams = new Type[] {typeof(int)
,
                   typeof(int)};
     
       AppDomain myDomain = Thread.GetDomain();
       AssemblyName myAsmName = new AssemblyName();
       myAsmName.Name = "MyDynamicAssembly";

       AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
                      myAsmName, 
                      AssemblyBuilderAccess.RunAndSave);

       ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule("PointModule"
,
                                    "Point.dll");

       TypeBuilder pointTypeBld = pointModule.DefineType("Point",
                                  TypeAttributes.Public);

       FieldBuilder xField = pointTypeBld.DefineField("x", typeof(int)
,
                                                      FieldAttributes.Public);
       FieldBuilder yField = pointTypeBld.DefineField("y", typeof(int),
 
                                                      FieldAttributes.Public);


       Type objType = Type.GetType("System.Object"); 
       ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);

       ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor(
                                    MethodAttributes.Public,
                                   CallingConventions.Standard,
                                   ctorParams);
       ILGenerator ctorIL = pointCtor.GetILGenerator();


       // First, you build the constructor.
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Call, objCtor);
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Ldarg_1);
       ctorIL.Emit(OpCodes.Stfld, xField); 
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Ldarg_2);
       ctorIL.Emit(OpCodes.Stfld, yField); 
       ctorIL.Emit(OpCodes.Ret); 

       //  Now, you'll build a method to output some information on
 the
       // inside your dynamic class. This method will have the following
       // definition in C#:
    //  public void WritePoint()
      
       MethodBuilder writeStrMthd = pointTypeBld.DefineMethod(
                                     "WritePoint", 
                             MethodAttributes.Public,
                                             typeof(void), 
                                             null);

       
       ILGenerator writeStrIL = writeStrMthd.GetILGenerator();
      
       // The below ILGenerator created demonstrates a few ways to create
       // string output through STDIN. 

       // ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
       // call to WriteLine for you.

       writeStrIL.EmitWriteLine("The value of this current
 instance is:");

       // Here, you will do the hard work yourself. First, you need
 to create
       // the string we will be passing and obtain the correct WriteLine
 overload
       // for said string. In the below case, you are substituting in
 two values,
       // so the chosen overload is Console.WriteLine(string, object,
 object).

       String inStr = "({0}, {1})";
       Type[] wlParams = new Type[] {typeof(string)
,
                     typeof(object),
                     typeof(object)};

       // We need the MethodInfo to pass into EmitCall later.

       MethodInfo writeLineMI = typeof(Console).GetMethod(
                            "WriteLine",
                        wlParams);

       // Push the string with the substitutions onto the stack.
       // This is the first argument for WriteLine - the string one.
 

       writeStrIL.Emit(OpCodes.Ldstr, inStr);

       // Since the second argument is an object, and it corresponds
 to
       // to the substitution for the value of our integer field, you
 
       // need to box that field to an object. First, push a reference
       // to the current instance, and then push the value stored in
       // field 'x'. We need the reference to the current instance (stored
       // in local argument index 0) so Ldfld can load from the correct
       // instance (this one).

       writeStrIL.Emit(OpCodes.Ldarg_0);
       writeStrIL.Emit(OpCodes.Ldfld, xField);

       // Now, we execute the box opcode, which pops the value of field
 'x',
       // returning a reference to the integer value boxed as an object.

       writeStrIL.Emit(OpCodes.Box, typeof(int));

       // Atop the stack, you'll find our string inStr, followed by
 a reference
       // to the boxed value of 'x'. Now, you need to likewise box field
 'y'.

       writeStrIL.Emit(OpCodes.Ldarg_0);
       writeStrIL.Emit(OpCodes.Ldfld, yField);
       writeStrIL.Emit(OpCodes.Box, typeof(int));

       // Now, you have all of the arguments for your call to
       // Console.WriteLine(string, object, object) atop the stack:
       // the string InStr, a reference to the boxed value of 'x', and
       // a reference to the boxed value of 'y'.

       // Call Console.WriteLine(string, object, object) with EmitCall.

       writeStrIL.EmitCall(OpCodes.Call, writeLineMI, null);

       // Lastly, EmitWriteLine can also output the value of a field
       // using the overload EmitWriteLine(FieldInfo).

       writeStrIL.EmitWriteLine("The value of 'x' is:");
       writeStrIL.EmitWriteLine(xField);
       writeStrIL.EmitWriteLine("The value of 'y' is:");
       writeStrIL.EmitWriteLine(yField);

       // Since we return no value (void), the the ret opcode will not
       // return the top stack value.

       writeStrIL.Emit(OpCodes.Ret);
      
       return pointTypeBld.CreateType();

   }

   public static void Main()
 {

      object[] ctorParams = new object[2];

      Console.Write("Enter a integer value for X: ");
 
      string myX = Console.ReadLine();
      Console.Write("Enter a integer value for Y: ");
 
      string myY = Console.ReadLine();

      Console.WriteLine("---");

      ctorParams[0] = Convert.ToInt32(myX);
      ctorParams[1] = Convert.ToInt32(myY);

      Type ptType = CreateDynamicType();
  
      object ptInstance = Activator.CreateInstance(ptType, ctorParams);
      ptType.InvokeMember("WritePoint",
              BindingFlags.InvokeMethod,
              null,
              ptInstance,
              new object[0]);
   }
}

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ CreateDynamicType()
{
   array<Type^>^ctorParams = {int::typeid,int::typeid};
   AppDomain^ myDomain = Thread::GetDomain();
   AssemblyName^ myAsmName = gcnew AssemblyName;
   myAsmName->Name = "MyDynamicAssembly";
   AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName,
 AssemblyBuilderAccess::RunAndSave );
   ModuleBuilder^ pointModule = myAsmBuilder->DefineDynamicModule( "PointModule",
 "Point.dll" );
   TypeBuilder^ pointTypeBld = pointModule->DefineType( "Point", TypeAttributes::Public
 );
   FieldBuilder^ xField = pointTypeBld->DefineField( "x", int::typeid,
 FieldAttributes::Public );
   FieldBuilder^ yField = pointTypeBld->DefineField( "y", int::typeid,
 FieldAttributes::Public );
   Type^ objType = Type::GetType( "System.Object" );
   ConstructorInfo^ objCtor = objType->GetConstructor( gcnew array<Type^>(0)
 );
   ConstructorBuilder^ pointCtor = pointTypeBld->DefineConstructor( MethodAttributes::Public,
 CallingConventions::Standard, ctorParams );
   ILGenerator^ ctorIL = pointCtor->GetILGenerator();
   
   // First, you build the constructor.
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Call, objCtor );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_1 );
   ctorIL->Emit( OpCodes::Stfld, xField );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_2 );
   ctorIL->Emit( OpCodes::Stfld, yField );
   ctorIL->Emit( OpCodes::Ret );
   
   //  Now, you'll build a method to output some information on the
   // inside your dynamic class. This method will have the following
   // definition in C#:
   //  public void WritePoint()
   MethodBuilder^ writeStrMthd = pointTypeBld->DefineMethod( "WritePoint",
 MethodAttributes::Public, void::typeid, nullptr );
   ILGenerator^ writeStrIL = writeStrMthd->GetILGenerator();
   
   // The below ILGenerator created demonstrates a few ways to create
   // String* output through STDIN.
   // ILGenerator::EmitWriteLine(String*) will generate a ldstr and
 a
   // call to WriteLine for you.
   writeStrIL->EmitWriteLine( "The value of this current
 instance is:" );
   
   // Here, you will do the hard work yourself. First, you need to create
   // the String* we will be passing and obtain the correct WriteLine
 overload
   // for said String*. In the below case, you are substituting in two
 values,
   // so the chosen overload is Console::WriteLine(String*, Object*,
 Object*).
   String^ inStr = "( {0}, {1})";
   array<Type^>^wlParams = {String::typeid,Object::typeid,Object::typeid};
   
   // We need the MethodInfo to pass into EmitCall later.
   MethodInfo^ writeLineMI = Console::typeid->GetMethod( "WriteLine",
 wlParams );
   
   // Push the String* with the substitutions onto the stack.
   // This is the first argument for WriteLine - the String* one.
   writeStrIL->Emit( OpCodes::Ldstr, inStr );
   
   // Since the second argument is an Object*, and it corresponds to
   // to the substitution for the value of our integer field, you
   // need to box that field to an Object*. First, push a reference
   // to the current instance, and then push the value stored in
   // field 'x'. We need the reference to the current instance (stored
   // in local argument index 0) so Ldfld can load from the correct
   // instance (this one).
   writeStrIL->Emit( OpCodes::Ldarg_0 );
   writeStrIL->Emit( OpCodes::Ldfld, xField );
   
   // Now, we execute the box opcode, which pops the value of field
 'x',
   // returning a reference to the integer value boxed as an Object*.
   writeStrIL->Emit( OpCodes::Box, int::typeid );
   
   // Atop the stack, you'll find our String* inStr, followed by a reference
   // to the boxed value of 'x'. Now, you need to likewise box field
 'y'.
   writeStrIL->Emit( OpCodes::Ldarg_0 );
   writeStrIL->Emit( OpCodes::Ldfld, yField );
   writeStrIL->Emit( OpCodes::Box, int::typeid );
   
   // Now, you have all of the arguments for your call to
   // Console::WriteLine(String*, Object*, Object*) atop the stack:
   // the String* InStr, a reference to the boxed value of 'x', and
   // a reference to the boxed value of 'y'.
   // Call Console::WriteLine(String*, Object*, Object*) with EmitCall.
   writeStrIL->EmitCall( OpCodes::Call, writeLineMI, nullptr );
   
   // Lastly, EmitWriteLine can also output the value of a field
   // using the overload EmitWriteLine(FieldInfo).
   writeStrIL->EmitWriteLine( "The value of 'x' is:" );
   writeStrIL->EmitWriteLine( xField );
   writeStrIL->EmitWriteLine( "The value of 'y' is:" );
   writeStrIL->EmitWriteLine( yField );
   
   // Since we return no value (void), the the ret opcode will not
   // return the top stack value.
   writeStrIL->Emit( OpCodes::Ret );
   return pointTypeBld->CreateType();
}

int main()
{
   array<Object^>^ctorParams = gcnew array<Object^>(2);
   Console::Write( "Enter a integer value for X: " );
   String^ myX = Console::ReadLine();
   Console::Write( "Enter a integer value for Y: " );
   String^ myY = Console::ReadLine();
   Console::WriteLine( "---" );
   ctorParams[ 0 ] = Convert::ToInt32( myX );
   ctorParams[ 1 ] = Convert::ToInt32( myY );
   Type^ ptType = CreateDynamicType();
   Object^ ptInstance = Activator::CreateInstance( ptType, ctorParams );
   ptType->InvokeMember( "WritePoint", BindingFlags::InvokeMethod, nullptr,
 ptInstance, gcnew array<Object^>(0) );
}

import System.*;
import System.Threading.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

class EmitWriteLineDemo
{
    public static Type CreateDynamicType()
    {
        Type ctorParams[] = new Type[] { int.class.ToType(),
 
            int.class.ToType() };
        AppDomain myDomain = System.Threading.Thread.GetDomain();
        AssemblyName myAsmName = new AssemblyName();
        myAsmName.set_Name("MyDynamicAssembly");
        AssemblyBuilder myAsmBuilder = 
            myDomain.DefineDynamicAssembly(myAsmName,
            AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule(
            "PointModule", "Point.dll");
        TypeBuilder pointTypeBld = pointModule.DefineType("Point",
            TypeAttributes.Public);
        FieldBuilder xField = pointTypeBld.DefineField("x", int.class.ToType()
,
            FieldAttributes.Public);
        FieldBuilder yField = pointTypeBld.DefineField("y", int.class.ToType()
,
            FieldAttributes.Public);
        Type objType = Type.GetType("System.Object");
        ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);
        ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor
            (MethodAttributes.Public, CallingConventions.Standard, ctorParams);
        ILGenerator ctorIL = pointCtor.GetILGenerator();

        // First, you build the constructor.
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Call, objCtor);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_1);
        ctorIL.Emit(OpCodes.Stfld, xField);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_2);
        ctorIL.Emit(OpCodes.Stfld, yField);
        ctorIL.Emit(OpCodes.Ret);

        //  Now, you'll build a method to output some information on
 the
        // inside your dynamic class. This method will have the following
        // definition in VJ#:
        //  public void WritePoint()
        MethodBuilder writeStrMthd = pointTypeBld.DefineMethod("WritePoint"
,
            MethodAttributes.Public, void.class.ToType(),
 null);
        ILGenerator writeStrIL = writeStrMthd.GetILGenerator();

        // The below ILGenerator created demonstrates a few ways to
 create
        // string output through STDIN. 
        // ILGenerator.EmitWriteLine(string) will generate a ldstr and
 a 
        // call to WriteLine for you.
        writeStrIL.EmitWriteLine("The value of this current
 instance is:");

        // Here, you will do the hard work yourself. First, you need
 to create
        // the string we will be passing and obtain the correct WriteLine
        // overload for said string. In the below case, you are substituting
        // in two values, so the chosen overload is
        // Console.WriteLine(string, object, object).
        String inStr = "({0}, {1})";
        Type wlParams[] = new Type[] { String.class.ToType()
,
            Object.class.ToType(), Object.class.ToType()
 };

        // We need the MethodInfo to pass into EmitCall later.
        MethodInfo writeLineMI = Console.class.ToType().GetMethod("WriteLine"
,
            wlParams);

        // Push the string with the substitutions onto the stack.
        // This is the first argument for WriteLine - the string one.
 
        writeStrIL.Emit(OpCodes.Ldstr, inStr);

        // Since the second argument is an object, and it corresponds
 to
        // to the substitution for the value of our integer field, you
 
        // need to box that field to an object. First, push a reference
        // to the current instance, and then push the value stored in
        // field 'x'. We need the reference to the current instance
 (stored
        // in local argument index 0) so Ldfld can load from the correct
        // instance (this one).
        writeStrIL.Emit(OpCodes.Ldarg_0);
        writeStrIL.Emit(OpCodes.Ldfld, xField);

        // Now, we execute the box opcode, which pops the value of field
 'x',
        // returning a reference to the integer value boxed as an object.
        writeStrIL.Emit(OpCodes.Box, int.class.ToType());

        // Atop the stack, you'll find our string inStr, followed by
 a 
        // reference to the boxed value of 'x'. Now, you need to likewise
        // box field 'y'.
        writeStrIL.Emit(OpCodes.Ldarg_0);
        writeStrIL.Emit(OpCodes.Ldfld, yField);
        writeStrIL.Emit(OpCodes.Box, int.class.ToType());

        // Now, you have all of the arguments for your call to
        // Console.WriteLine(string, object, object) atop the stack:
        // the string InStr, a reference to the boxed value of 'x',
 and
        // a reference to the boxed value of 'y'.
        // Call Console.WriteLine(string, object, object) with EmitCall.
        writeStrIL.EmitCall(OpCodes.Call, writeLineMI, null);

        // Lastly, EmitWriteLine can also output the value of a field
        // using the overload EmitWriteLine(FieldInfo).
        writeStrIL.EmitWriteLine("The value of 'x' is:");
        writeStrIL.EmitWriteLine(xField);
        writeStrIL.EmitWriteLine("The value of 'y' is:");
        writeStrIL.EmitWriteLine(yField);

        // Since we return no value (void), the the ret opcode will
 not
        // return the top stack value.
        writeStrIL.Emit(OpCodes.Ret);
        return pointTypeBld.CreateType();
    } //CreateDynamicType
    
    public static void main(String[]
 args)
    {
        Object ctorParams[] = new Object[2];
        Console.Write("Enter a integer value for X: ");
        String myX = Console.ReadLine();
        Console.Write("Enter a integer value for Y: ");
        String myY = Console.ReadLine();
        Console.WriteLine("---");
        ctorParams[0] = (Int32)Integer.parseInt(myX);
        ctorParams[1] = (Int32)Integer.parseInt(myY);
        Type ptType = CreateDynamicType();
        Object ptInstance = Activator.CreateInstance(ptType, ctorParams);
        ptType.InvokeMember("WritePoint", BindingFlags.InvokeMethod, null
,
            ptInstance, new Object[0]);
    } //main
} //EmitWriteLineDemo
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ILGenerator クラス
ILGenerator メンバ
System.Reflection.Emit 名前空間

ILGenerator.EmitWriteLine メソッド

異な種類の値を使用して WriteLine への呼び出し生成するヘルパ関数
オーバーロードの一覧オーバーロードの一覧

名前 説明
ILGenerator.EmitWriteLine (FieldInfo) 指定したフィールド使用して System.Console.WriteLine を呼び出すために必要な MSIL生成します
ILGenerator.EmitWriteLine (LocalBuilder) 指定したローカル変数使用して System.Console.WriteLine呼び出すために必要な MSIL生成します
ILGenerator.EmitWriteLine (String) 文字列使用して System.Console.WriteLine呼び出す MSIL生成します
参照参照

関連項目

ILGenerator クラス
ILGenerator メンバ
System.Reflection.Emit 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ILGenerator.EmitWriteLine メソッド」の関連用語

ILGenerator.EmitWriteLine メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ILGenerator.EmitWriteLine メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS