This documentation is archived and is not being maintained.

LocalBuilder.SetLocalSymInfo Method

Sets the local variable's symbolic information.

Overload List

Sets the name of this local variable.

[Visual Basic] Overloads Public Sub SetLocalSymInfo(String)
[C#] public void SetLocalSymInfo(string);
[C++] public: void SetLocalSymInfo(String*);
[JScript] public function SetLocalSymInfo(String);

Sets the name and lexical scope of this local variable.

[Visual Basic] Overloads Public Sub SetLocalSymInfo(String, Integer, Integer)
[C#] public void SetLocalSymInfo(string, int, int);
[C++] public: void SetLocalSymInfo(String*, int, int);
[JScript] public function SetLocalSymInfo(String, int, int);

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of SetSymLocalInfo.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SetLocalSymInfo. For other examples that might be available, see the individual overload topics.
[Visual Basic] 

         ' 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.")

[C#] 

         // 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.");

[C++] 

      // Create an assembly.
      AssemblyName* myAssemblyName = new AssemblyName();
      myAssemblyName->Name = S"SampleAssembly";

      AssemblyBuilder* myAssembly = Thread::GetDomain()->DefineDynamicAssembly(
         myAssemblyName, AssemblyBuilderAccess::Run);

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

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

      // Define a private String field.
      FieldBuilder* myField =
         myTypeBuilder->DefineField(S"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(S"Function1",
         MethodAttributes::Public, __typeof(String), 0);

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

      // Create local variables.
      LocalBuilder* myLB1 = myMethodIL->DeclareLocal(__typeof(String));
      myLB1->SetLocalSymInfo(S"myString");
      Console::WriteLine(S"'myLB1' type is :{0}", myLB1->LocalType);
      LocalBuilder* myLB2 = myMethodIL->DeclareLocal(__typeof(int));
      myLB2->SetLocalSymInfo(S"myInt",1,2);
      Console::WriteLine(S"'myLB2' type is :{0}", myLB2->LocalType);
      myMethodIL->Emit(OpCodes::Ldstr, S"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(S"'myClass' is created.");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

LocalBuilder Class | LocalBuilder Members | System.Reflection.Emit Namespace

Show: