FieldBuilder Class
Defines and represents a field. This class cannot be inherited.
System.Reflection.MemberInfo
System.Reflection.FieldInfo
System.Reflection.Emit.FieldBuilder
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
The FieldBuilder type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Attributes | Gets the attributes of this field. (Overrides FieldInfo.Attributes.) |
![]() | DeclaringType | Gets a reference to the Type object for the type that declares this field. (Overrides MemberInfo.DeclaringType.) |
![]() | FieldHandle | Gets the internal metadata handle for this field. (Overrides FieldInfo.FieldHandle.) |
![]() | FieldType | Gets the Type object that represents the type of this field. (Overrides FieldInfo.FieldType.) |
![]() | IsAssembly | Gets a value that indicates whether the potential visibility of this field is described by FieldAttributes.Assembly; that is, the field is visible at most to other types in the same assembly, and is not visible to derived types outside the assembly. (Inherited from FieldInfo.) |
![]() | IsFamily | Gets a value that indicates whether the visibility of this field is described by FieldAttributes.Family; that is, the field is visible only within its class and derived classes. (Inherited from FieldInfo.) |
![]() | IsFamilyAndAssembly | Gets a value that indicates whether the visibility of this field is described by FieldAttributes.FamANDAssem; that is, the field can be accessed from derived classes, but only if they are in the same assembly. (Inherited from FieldInfo.) |
![]() | IsFamilyOrAssembly | Gets a value that indicates whether the potential visibility of this field is described by FieldAttributes.FamORAssem; that is, the field can be accessed by derived classes wherever they are, and by classes in the same assembly. (Inherited from FieldInfo.) |
![]() | IsInitOnly | Gets a value that indicates whether the field can be set only in the body of the constructor. (Inherited from FieldInfo.) |
![]() | IsLiteral | Gets a value that indicates whether the value is written at compile time and cannot be changed. (Inherited from FieldInfo.) |
![]() | IsNotSerialized | Gets a value that indicates whether this field has the NotSerialized attribute. (Inherited from FieldInfo.) |
![]() | IsPinvokeImpl | Gets a value that indicates whether the corresponding PinvokeImpl attribute is set in FieldAttributes. (Inherited from FieldInfo.) |
![]() | IsPrivate | Gets a value that indicates whether the field is private. (Inherited from FieldInfo.) |
![]() | IsPublic | Gets a value that indicates whether the field is public. (Inherited from FieldInfo.) |
![]() | IsSpecialName | Gets a value that indicates whether the field has a name that has special significance. (Inherited from FieldInfo.) |
![]() | IsStatic | Gets a value that indicates whether the field is static (Shared in Visual Basic). (Inherited from FieldInfo.) |
![]() | MemberType | Gets a value that indicates that this member is a field. (Inherited from FieldInfo.) |
![]() | MetadataToken | Gets a value that identifies a metadata element. (Inherited from MemberInfo.) |
![]() | Module | Gets the module in which the type that contains this field is being defined. (Overrides MemberInfo.Module.) |
![]() | Name | Gets the name of this field. (Overrides MemberInfo.Name.) |
![]() | ReflectedType | Gets the reference to the Type object from which this object was obtained. (Overrides MemberInfo.ReflectedType.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetCustomAttributes(Boolean) | Returns all the custom attributes defined for this field. (Overrides MemberInfo.GetCustomAttributes(Boolean).) |
![]() | GetCustomAttributes(Type, Boolean) | Returns all the custom attributes defined for this field identified by the given type. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetRawConstantValue | Returns a literal value associated with the field by a compiler. (Inherited from FieldInfo.) |
![]() | GetToken | Returns the token representing this field. |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | GetValue | Retrieves the value of the field supported by the given object. (Overrides FieldInfo.GetValue(Object).) |
![]() | IsDefined | Gets a value that indicates whether an attribute having the specified type is defined on a field. (Overrides MemberInfo.IsDefined(Type, Boolean).) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | SetConstant | Sets the default value of this field. |
![]() | SetCustomAttribute | Sets a custom attribute using a custom attribute builder. |
![]() | SetValue(Object, Object) | Sets the value of the field that is supported by the given object. (Inherited from FieldInfo.) |
![]() | SetValue(Object, Object, BindingFlags, Binder, CultureInfo) | Sets the value of the field supported by the given object. (Overrides FieldInfo.SetValue(Object, Object, BindingFlags, Binder, CultureInfo).) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
Get an instance of FieldBuilder by calling the TypeBuilder.DefineField method.
The following code sample illustrates the use of FieldBuilder.
Note: |
|---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
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"; } }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.



Note: