Export (0) Print
Expand All

FieldBuilder.Attributes Property

Gets the attributes of this field.

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

public override FieldAttributes Attributes { get; }

Property Value

Type: System.Reflection.FieldAttributes
The attributes of this field.

The following example creates a field in a dynamic type, and displays its attributes by using the Attributes property.


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

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName("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 myConstructor = 
         myTypeBuilder.DefineConstructor(MethodAttributes.Public, 
            CallingConventions.Standard, constructorArgs);
      ILGenerator constructorIL = myConstructor.GetILGenerator();
      constructorIL.Emit(OpCodes.Ldarg_0);
      ConstructorInfo superConstructor = typeof(object).GetConstructor(new Type[]{});
      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);

      if (0 != (myFieldBuilder.Attributes & FieldAttributes.Static))
      {
         outputBlock.Text += "MyField defined as Static\n";
      }

      FieldAttributes access = 
         myFieldBuilder.Attributes & FieldAttributes.FieldAccessMask;
      if (access == FieldAttributes.Private)
      {
         outputBlock.Text += "MyField is private\n";
      }

      outputBlock.Text += "ReflectedType of FieldBuilder is: " + 
         myFieldBuilder.ReflectedType.ToString() + "\n";

      Type myType = myTypeBuilder.CreateType();

      object helloWorld = Activator.CreateInstance(myType, new object[]{"HelloWorld"});

      // Invoke the "MyMethod"  of the "MyClass".
      object myObject = myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, 
         null, helloWorld, null);
      outputBlock.Text += "MyClass.MyMethod returned: \"" + myObject.ToString() + "\"\n";
   }
}

/* This example produces the following output:

MyField defined as Static 
MyField is private
ReflectedType of FieldBuilder is: MyClass
MyClass.MyMethod returned: "HelloWorld"
 */


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