LocalBuilder クラスとは? わかりやすく解説

LocalBuilder クラス

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

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

<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class
 LocalBuilder
    Inherits LocalVariableInfo
    Implements _LocalBuilder
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public sealed class LocalBuilder : LocalVariableInfo,
 _LocalBuilder
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
public ref class LocalBuilder sealed : public
 LocalVariableInfo, _LocalBuilder
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public final class LocalBuilder extends LocalVariableInfo
 implements _LocalBuilder
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
public final class LocalBuilder extends
 LocalVariableInfo implements _LocalBuilder
解説解説

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

使用例使用例

LocalBuilder使用する例を次に示します

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

Class LocalBuilder_Sample

   Public Shared Sub Main()
      Try

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

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

         ' Create a module 'myModule'.
         Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("SampleModule",
 True)

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

         ' Define a private String field.
         Dim myField As FieldBuilder = myTypeBuilder.DefineField("myMessage",
 GetType(String), _
                                                                        FieldAttributes.Private)

         ' Create the constructor.
         Dim myConstructorArgs As Type() =
 {GetType(String)}
         Dim myConstructor As ConstructorBuilder
 = myTypeBuilder.DefineConstructor _
                           (MethodAttributes.Public, CallingConventions.Standard,
 myConstructorArgs)

         ' Generate IL for the method.
         Dim myConstructorIL As ILGenerator
 = myConstructor.GetILGenerator()
         myConstructorIL.Emit(OpCodes.Ldarg_0)
         Dim mySuperConstructor As ConstructorInfo
 = GetType(Object).GetConstructor(New
 Type() {})
         myConstructorIL.Emit(OpCodes.Call, mySuperConstructor)
         myConstructorIL.Emit(OpCodes.Ldarg_0)
         myConstructorIL.Emit(OpCodes.Ldarg_1)
         myConstructorIL.Emit(OpCodes.Stfld, myField)
         myConstructorIL.Emit(OpCodes.Ret)

         ' Create the 'Function1' public method.
         Dim myMethod As MethodBuilder = myTypeBuilder.DefineMethod("Function1",
 MethodAttributes. _
                                                   Public, GetType(String),
 Nothing)

         ' Generate IL for 'Function1'.
         Dim myMethodIL As ILGenerator = myMethod.GetILGenerator()

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

         myMethodIL.Emit(OpCodes.Ldstr, "Local value"
  )
         myMethodIL.Emit(OpCodes.Stloc_0 )
         myMethodIL.Emit(OpCodes.Ldloc_0 )
         myMethodIL.Emit(OpCodes.Stloc_1)
         myMethodIL.Emit(OpCodes.Ldloc_1 )
         myMethodIL.Emit(OpCodes.Ret )

         ' Create "myClass" class.
         Dim myType1 As Type = myTypeBuilder.CreateType()

         Console.WriteLine("'myClass' is created.")
         ' Create an instance of the 'myClass'.
         Dim myObject1 As Object
 = Activator.CreateInstance(myType1, New Object()
 {"HelloWorld"})

         ' Invoke 'Function1' method of 'myClass'.
         Dim myObject2 As Object
 = myType1.InvokeMember("Function1", BindingFlags.InvokeMethod,
 _
                                                                     Nothing,
 myObject1, Nothing)
         Console.WriteLine("myClass.Function1 is called.")
         Console.WriteLine("myClass.Function1 returned: {0}",
 myObject2)
      Catch e As Exception
         Console.WriteLine("Exception :{0}", e.Message)
      End Try
   End Sub 'Main
End Class 'LocalBuilder_Sample
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

class LocalBuilder_Sample
{
   public static void Main()
   {
      try
      {

         // Create an assembly.
         AssemblyName myAssemblyName = new AssemblyName();
         myAssemblyName.Name = "SampleAssembly";

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

         // Create a module 'myModule'.
         ModuleBuilder myModule=myAssembly.DefineDynamicModule("SampleModule"
,true);

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

         // Define a private String field.
         FieldBuilder myField = myTypeBuilder.DefineField("myMessage",
 typeof(String), FieldAttributes.Private);

         // Create the constructor.
         Type[] myConstructorArgs = { typeof(String) };
         ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
            MethodAttributes.Public, CallingConventions.Standard, myConstructorArgs);

         // Generate IL for the method.
         ILGenerator myConstructorIL = myConstructor.GetILGenerator();
         myConstructorIL.Emit(OpCodes.Ldarg_0);
         ConstructorInfo mySuperConstructor = typeof(Object).GetConstructor(new
 Type[0]);
         myConstructorIL.Emit(OpCodes.Call, mySuperConstructor);
         myConstructorIL.Emit(OpCodes.Ldarg_0);
         myConstructorIL.Emit(OpCodes.Ldarg_1);
         myConstructorIL.Emit(OpCodes.Stfld, myField);
         myConstructorIL.Emit(OpCodes.Ret);

         // Create the 'Function1' public method.
         MethodBuilder myMethod = myTypeBuilder.DefineMethod("Function1"
,
            MethodAttributes.Public, typeof(String), null);

         // Generate IL for 'Function1'.
         ILGenerator myMethodIL = myMethod.GetILGenerator();

         // Create local variables.
         LocalBuilder myLB1 = myMethodIL.DeclareLocal(typeof(string));
         myLB1.SetLocalSymInfo("myString");
         Console.WriteLine("'myLB1' type is :{0}", myLB1.LocalType);
         LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(int));
         myLB2.SetLocalSymInfo("myInt",1,2);
         Console.WriteLine("'myLB2' type is :{0}", myLB2.LocalType);
         myMethodIL.Emit(OpCodes.Ldstr, "Local value"  );
         myMethodIL.Emit(OpCodes.Stloc_0 );
         myMethodIL.Emit(OpCodes.Ldloc_0 );
         myMethodIL.Emit(OpCodes.Stloc_1);
         myMethodIL.Emit(OpCodes.Ldloc_1 );
         myMethodIL.Emit(OpCodes.Ret );

         // Create "myClass" class.
         Type myType1 = myTypeBuilder.CreateType();



         Console.WriteLine("'myClass' is created.");
         // Create an instance of the 'myClass'.
         Object myObject1 = Activator.CreateInstance(myType1, new
 Object[] { "HelloWorld" });

         // Invoke 'Function1' method of 'myClass'.
         Object myObject2 = myType1.InvokeMember("Function1", BindingFlags.InvokeMethod,
 null, myObject1, null);
         Console.WriteLine("myClass.Function1 is called.");
         Console.WriteLine("myClass.Function1 returned: {0}", myObject2);
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception :{0}", e.Message );
      }
   }
}
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Threading;
int main()
{
   try
   {
      // Create an assembly.
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "SampleAssembly";
      AssemblyBuilder^ myAssembly = Thread::GetDomain()->DefineDynamicAssembly(
 myAssemblyName, AssemblyBuilderAccess::Run );

      // Create a module 'myModule'.
      ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "SampleModule",
 true );

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

      // Define a private String field.
      FieldBuilder^ myField = myTypeBuilder->DefineField( "myMessage",
 String::typeid, FieldAttributes::Private );

      // Create the constructor.
      array<Type^>^myConstructorArgs = {String::typeid};
      ConstructorBuilder^ myConstructor = myTypeBuilder->DefineConstructor( MethodAttributes::Public,
 CallingConventions::Standard, myConstructorArgs );

      // Generate IL for the method.
      ILGenerator^ myConstructorIL = myConstructor->GetILGenerator();
      myConstructorIL->Emit( OpCodes::Ldarg_0 );
      ConstructorInfo^ mySuperConstructor = Object::typeid->GetConstructor( gcnew
 array<Type^>(0) );
      myConstructorIL->Emit( OpCodes::Call, mySuperConstructor );
      myConstructorIL->Emit( OpCodes::Ldarg_0 );
      myConstructorIL->Emit( OpCodes::Ldarg_1 );
      myConstructorIL->Emit( OpCodes::Stfld, myField );
      myConstructorIL->Emit( OpCodes::Ret );

      // Create the 'Function1' public method.
      MethodBuilder^ myMethod = myTypeBuilder->DefineMethod( "Function1",
 MethodAttributes::Public, String::typeid, nullptr );

      // Generate IL for 'Function1'.
      ILGenerator^ myMethodIL = myMethod->GetILGenerator();

      // Create local variables.
      LocalBuilder^ myLB1 = myMethodIL->DeclareLocal( String::typeid );
      myLB1->SetLocalSymInfo( "myString" );
      Console::WriteLine( "'myLB1' type is :{0}", myLB1->LocalType );
      LocalBuilder^ myLB2 = myMethodIL->DeclareLocal( int::typeid
 );
      myLB2->SetLocalSymInfo( "myInt", 1, 2 );
      Console::WriteLine( "'myLB2' type is :{0}", myLB2->LocalType );
      myMethodIL->Emit( OpCodes::Ldstr, "Local value" );
      myMethodIL->Emit( OpCodes::Stloc_0 );
      myMethodIL->Emit( OpCodes::Ldloc_0 );
      myMethodIL->Emit( OpCodes::Stloc_1 );
      myMethodIL->Emit( OpCodes::Ldloc_1 );
      myMethodIL->Emit( OpCodes::Ret );

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

      // Create an instance of the 'myClass'.
      array<Object^>^temp0 = {"HelloWorld"};
      Object^ myObject1 = Activator::CreateInstance( myType1, temp0 );

      // Invoke 'Function1' method of 'myClass'.
      Object^ myObject2 = myType1->InvokeMember( "Function1", BindingFlags::InvokeMethod,
 nullptr, myObject1, nullptr );
      Console::WriteLine( "myClass.Function1 is called." );
      Console::WriteLine( "myClass.Function1 returned: {0}", myObject2
 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception :{0}", e->Message );
   }
}
継承階層継承階層
System.Object
   System.Reflection.LocalVariableInfo
    System.Reflection.Emit.LocalBuilder
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
LocalBuilder メンバ
System.Reflection.Emit 名前空間



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

辞書ショートカット

すべての辞書の索引

「LocalBuilder クラス」の関連用語

LocalBuilder クラスのお隣キーワード
検索ランキング

   

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



LocalBuilder クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS