次の方法で共有


LocalBuilder クラス

定義

メソッドまたはコンストラクター内のローカル変数を表します。

public ref class LocalBuilder sealed : System::Reflection::LocalVariableInfo
public ref class LocalBuilder abstract : System::Reflection::LocalVariableInfo
public ref class LocalBuilder sealed : System::Runtime::InteropServices::_LocalBuilder
public ref class LocalBuilder sealed : System::Reflection::LocalVariableInfo, System::Runtime::InteropServices::_LocalBuilder
public sealed class LocalBuilder : System.Reflection.LocalVariableInfo
public abstract class LocalBuilder : System.Reflection.LocalVariableInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class LocalBuilder : System.Runtime.InteropServices._LocalBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class LocalBuilder : System.Reflection.LocalVariableInfo, System.Runtime.InteropServices._LocalBuilder
type LocalBuilder = class
    inherit LocalVariableInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type LocalBuilder = class
    interface _LocalBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type LocalBuilder = class
    inherit LocalVariableInfo
    interface _LocalBuilder
Public NotInheritable Class LocalBuilder
Inherits LocalVariableInfo
Public MustInherit Class LocalBuilder
Inherits LocalVariableInfo
Public NotInheritable Class LocalBuilder
Implements _LocalBuilder
Public NotInheritable Class LocalBuilder
Inherits LocalVariableInfo
Implements _LocalBuilder
継承
LocalBuilder
継承
LocalBuilder
属性
実装

次の例では、文字列を返し、Int32型のパラメーターを持つ Function1 という名前の static メソッド (Visual Basic のShared) を作成します。 メソッドの本体で、コード例では、2 つのローカル変数を表す LocalBuilder オブジェクトを作成し、ローカル変数のシンボル情報を設定します。 このメソッドは重要な処理を行いませんが、メソッド本体では、ローカル変数にパラメーターを格納し、リテラル文字列をローカル変数に格納し、ローカル変数を読み込む方法を示します。

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Threading;
int main()
{
    // Create an assembly.
    AssemblyName^ myAssemblyName = gcnew AssemblyName;
    myAssemblyName->Name = "SampleAssembly";
    AssemblyBuilder^ myAssembly = Thread::GetDomain()->DefineDynamicAssembly( 
        myAssemblyName, AssemblyBuilderAccess::RunAndSave );

    // Create a module. For a single-file assembly the module
    // name is usually the same as the assembly name.
    ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( 
        myAssemblyName->Name, myAssemblyName->Name + ".dll", true );

    // Define a public class 'Example'.
    TypeBuilder^ myTypeBuilder = myModule->DefineType( "Example", TypeAttributes::Public );

    // Create the 'Function1' public method, which takes an integer
    // and returns a string.
    MethodBuilder^ myMethod = myTypeBuilder->DefineMethod( "Function1", 
        MethodAttributes::Public | MethodAttributes::Static, String::typeid, 
        gcnew array<Type^> { int::typeid } );

    // Generate IL for 'Function1'. The function body demonstrates
    // assigning an argument to a local variable, assigning a 
    // constant string to a local variable, and putting the contents
    // of local variables on the stack.
    ILGenerator^ myMethodIL = myMethod->GetILGenerator();

    // Create local variables named myString and myInt.
    LocalBuilder^ myLB1 = myMethodIL->DeclareLocal( String::typeid );
    myLB1->SetLocalSymInfo( "myString" );
    Console::WriteLine( "local 'myString' type is: {0}", myLB1->LocalType );

    LocalBuilder^ myLB2 = myMethodIL->DeclareLocal( int::typeid );
    myLB2->SetLocalSymInfo( "myInt", 1, 2 );
    Console::WriteLine( "local 'myInt' type is: {0}", myLB2->LocalType );

    // Store the function argument in myInt.
    myMethodIL->Emit( OpCodes::Ldarg_0 );
    myMethodIL->Emit( OpCodes::Stloc_1 );

    // Store a literal value in myString, and return the value.
    myMethodIL->Emit( OpCodes::Ldstr, "string value"  );
    myMethodIL->Emit( OpCodes::Stloc_0 );
    myMethodIL->Emit( OpCodes::Ldloc_0 );
    myMethodIL->Emit( OpCodes::Ret );

    // Create "Example" class.
    Type^ myType1 = myTypeBuilder->CreateType();
    Console::WriteLine( "'Example' is created." );

    myAssembly->Save(myAssemblyName->Name + ".dll");
    Console::WriteLine( "'{0}' is created.", myAssemblyName->Name + ".dll" );

    // Invoke 'Function1' method of 'Example', passing the value 42.
    Object^ myObject2 = myType1->InvokeMember( "Function1", 
        BindingFlags::InvokeMethod, nullptr, nullptr, 
        gcnew array<Object^> { 42 } );

    Console::WriteLine( "Example::Function1 returned: {0}", myObject2 );
}
/* This code example produces the following output:

local 'myString' type is: System.String
local 'myInt' type is: System.Int32
'Example' is created.
'SampleAssembly.dll' is created.
Example::Function1 returned: string value
 */
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

class LocalBuilder_Sample
{
    public static void Main()
    {
        // Create an assembly.
        AssemblyName myAssemblyName = new AssemblyName();
        myAssemblyName.Name = "SampleAssembly";

        AssemblyBuilder myAssembly =
           Thread.GetDomain().DefineDynamicAssembly(myAssemblyName,
               AssemblyBuilderAccess.RunAndSave);

        // Create a module. For a single-file assembly the module
        // name is usually the same as the assembly name.
        ModuleBuilder myModule =
            myAssembly.DefineDynamicModule(myAssemblyName.Name,
                myAssemblyName.Name + ".dll", true);

        // Define a public class 'Example'.
        TypeBuilder myTypeBuilder =
            myModule.DefineType("Example", TypeAttributes.Public);

        // Create the 'Function1' public method, which takes an integer
        // and returns a string.
        MethodBuilder myMethod = myTypeBuilder.DefineMethod("Function1",
           MethodAttributes.Public | MethodAttributes.Static,
           typeof(String), new Type[] { typeof(int) });

        // Generate IL for 'Function1'. The function body demonstrates
        // assigning an argument to a local variable, assigning a
        // constant string to a local variable, and putting the contents
        // of local variables on the stack.
        ILGenerator myMethodIL = myMethod.GetILGenerator();

        // Create local variables named myString and myInt.
        LocalBuilder myLB1 = myMethodIL.DeclareLocal(typeof(string));
        myLB1.SetLocalSymInfo("myString");
        Console.WriteLine("local 'myString' type is: {0}", myLB1.LocalType);

        LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(int));
        myLB2.SetLocalSymInfo("myInt", 1, 2);
        Console.WriteLine("local 'myInt' type is: {0}", myLB2.LocalType);

        // Store the function argument in myInt.
        myMethodIL.Emit(OpCodes.Ldarg_0 );
        myMethodIL.Emit(OpCodes.Stloc_1 );

        // Store a literal value in myString, and return the value.
        myMethodIL.Emit(OpCodes.Ldstr, "string value"  );
        myMethodIL.Emit(OpCodes.Stloc_0 );
        myMethodIL.Emit(OpCodes.Ldloc_0 );
        myMethodIL.Emit(OpCodes.Ret );

        // Create "Example" class.
        Type myType1 = myTypeBuilder.CreateType();
        Console.WriteLine("'Example' is created.");

        myAssembly.Save(myAssemblyName.Name + ".dll");
        Console.WriteLine( "'{0}' is created.", myAssemblyName.Name + ".dll" );

        // Invoke 'Function1' method of 'Example', passing the value 42.
        Object myObject2 = myType1.InvokeMember("Function1",
            BindingFlags.InvokeMethod, null, null, new Object[] { 42 });

        Console.WriteLine("Example.Function1 returned: {0}", myObject2);
    }
}
/* This code example produces the following output:

local 'myString' type is: System.String
local 'myInt' type is: System.Int32
'Example' is created.
'SampleAssembly.dll' is created.
Example.Function1 returned: string value
 */
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Threading

Class LocalBuilder_Sample

    Public Shared Sub Main()

        ' Create an assembly.
        Dim myAssemblyName As New AssemblyName()
        myAssemblyName.Name = "SampleAssembly"

        Dim myAssembly As AssemblyBuilder = _
            Thread.GetDomain().DefineDynamicAssembly( myAssemblyName, _
                             AssemblyBuilderAccess.RunAndSave )

        ' Create a module. For a single-file assembly the module
        ' name is usually the same as the assembly name.
        Dim myModule As ModuleBuilder = _
            myAssembly.DefineDynamicModule(myAssemblyName.Name, _
                myAssemblyName.Name & ".dll", True)

        ' Define a public class 'Example'.
        Dim myTypeBuilder As TypeBuilder = _
            myModule.DefineType("Example", TypeAttributes.Public)

        ' Create the 'Function1' public method, which takes an Integer
        ' and returns a string.
        Dim myMethod As MethodBuilder = myTypeBuilder.DefineMethod("Function1", _
            MethodAttributes.Public Or MethodAttributes.Static, _
            GetType(String), New Type() { GetType(Integer) })

        ' Generate IL for 'Function1'. The function body demonstrates
        ' assigning an argument to a local variable, assigning a 
        ' constant string to a local variable, and putting the contents
        ' of local variables on the stack.
        Dim myMethodIL As ILGenerator = myMethod.GetILGenerator()

        ' Create local variables named myString and myInt.
        Dim myLB1 As LocalBuilder = myMethodIL.DeclareLocal(GetType(String))
        myLB1.SetLocalSymInfo("myString")
        Console.WriteLine("local 'myString' type is: {0}", myLB1.LocalType)

        Dim myLB2 As LocalBuilder = myMethodIL.DeclareLocal(GetType(Integer))
        myLB2.SetLocalSymInfo("myInt", 1, 2)
        Console.WriteLine("local 'myInt' type is: {0}", myLB2.LocalType)

        ' Store the function argument in myInt.
        myMethodIL.Emit(OpCodes.Ldarg_0 )
        myMethodIL.Emit(OpCodes.Stloc_1 )

        ' Store a literal value in myString, and return the value.
        myMethodIL.Emit(OpCodes.Ldstr, "string value"  )
        myMethodIL.Emit(OpCodes.Stloc_0 )
        myMethodIL.Emit(OpCodes.Ldloc_0 )
        myMethodIL.Emit(OpCodes.Ret )

        ' Create "Example" class.
        Dim myType1 As Type = myTypeBuilder.CreateType()
        Console.WriteLine("'Example' is created.")

        myAssembly.Save(myAssemblyName.Name & ".dll")
        Console.WriteLine( "'{0}' is created.", myAssemblyName.Name & ".dll" )

        ' Invoke 'Function1' method of 'Example', passing the value 42.
        Dim myObject2 As Object = myType1.InvokeMember("Function1", _
            BindingFlags.InvokeMethod, Nothing, Nothing, New Object() { 42 })

        Console.WriteLine("Example.Function1 returned: {0}", myObject2)

   End Sub 
End Class 

' This code example produces the following output:
'
'local 'myString' type is: System.String
'local 'myInt' type is: System.Int32
''Example' is created.
''SampleAssembly.dll' is created.
'Example.Function1 returned: string value

注釈

LocalBuilder オブジェクトは、DeclareLocal メソッドを使用して定義できます。

コンストラクター

LocalBuilder()

LocalBuilder クラスの新しいインスタンスを初期化します。

プロパティ

IsPinned

ローカル変数によって参照されるオブジェクトがメモリに固定されているかどうかを示す値を取得します。

LocalIndex

メソッド本体内のローカル変数の 0 から始まるインデックスを取得します。

LocalType

ローカル変数の型を取得します。

メソッド

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
SetLocalSymInfo(String)

このローカル変数の名前を設定します。

SetLocalSymInfo(String, Int32, Int32)

このローカル変数の名前と字句スコープを設定します。

SetLocalSymInfoCore(String)

派生クラスでオーバーライドされた場合は、このローカル変数の名前を設定します。

ToString()

ローカル変数を記述するユーザーが読み取り可能な文字列を返します。

(継承元 LocalVariableInfo)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_LocalBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

名前のセットを、対応するディスパッチ識別子のセットにマップします。

_LocalBuilder.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。これを使用して、インターフェイスの型情報を取得できます。

_LocalBuilder.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数を取得します (0 または 1)。

_LocalBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

適用対象