Export (0) Print
Expand All

FieldBuilder.Name Property

Gets the name of this field.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public override string Name { get; }

Property Value

Type: System.String
A String containing the name of this field.

The following code sample illustrates the use of Name.


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

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "DynamicAssembly";
      AssemblyBuilder myAssembly =
         AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, 
               AssemblyBuilderAccess.Run);
      // Create a dynamic module in Dynamic Assembly.
      ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("MyModule");
      // Define a public class named "MyClass" in the assembly.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyClass", TypeAttributes.Public);

      // Define a private String field named "MyField" in the type.
      FieldBuilder myFieldBuilder = myTypeBuilder.DefineField("MyField",
          typeof(string), FieldAttributes.Private | FieldAttributes.Static);
      // Create the constructor.
      Type[] constructorArgs = { typeof(String) };
      ConstructorBuilder constructor = myTypeBuilder.DefineConstructor(
         MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
      ILGenerator constructorIL = constructor.GetILGenerator();
      constructorIL.Emit(OpCodes.Ldarg_0);
      ConstructorInfo superConstructor = typeof(Object).GetConstructor(new Type[0]);
      constructorIL.Emit(OpCodes.Call, superConstructor);
      constructorIL.Emit(OpCodes.Ldarg_0);
      constructorIL.Emit(OpCodes.Ldarg_1);
      constructorIL.Emit(OpCodes.Stfld, myFieldBuilder);
      constructorIL.Emit(OpCodes.Ret);

      // Create the MyMethod method.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("MyMethod",
                           MethodAttributes.Public, typeof(String), null);
      ILGenerator methodIL = myMethodBuilder.GetILGenerator();
      methodIL.Emit(OpCodes.Ldarg_0);
      methodIL.Emit(OpCodes.Ldfld, myFieldBuilder);
      methodIL.Emit(OpCodes.Ret);
      outputBlock.Text += "Name: " + myFieldBuilder.Name + "\n";
      outputBlock.Text += "DeclaringType: " + myFieldBuilder.DeclaringType + "\n";
      outputBlock.Text += "Type: " + myFieldBuilder.FieldType + "\n";
      outputBlock.Text += "Token: " + myFieldBuilder.GetToken().Token + "\n";

      Type myType = myTypeBuilder.CreateType();
      // Create an instance of the "HelloWorld" class.
      Object helloWorld = Activator.CreateInstance(myType, new Object[] { "HelloWorld" });
      // Invoke the "MyMethod" method of the "MyClass" class.
      Object myObject = myType.InvokeMember("MyMethod",
                        BindingFlags.InvokeMethod, null, helloWorld, null);
      outputBlock.Text += "MyClass.MyMethod returned: \"" + myObject + "\"" + "\n";
   }
}


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft