FieldBuilder Class
.NET Framework 3.0
Defines and represents a field. This class cannot be inherited.
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
[ClassInterfaceAttribute(ClassInterfaceType.None)] [ComVisibleAttribute(true)] public sealed class FieldBuilder : FieldInfo, _FieldBuilder
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ /** @attribute ComVisibleAttribute(true) */ public final class FieldBuilder extends FieldInfo implements _FieldBuilder
ClassInterfaceAttribute(ClassInterfaceType.None) ComVisibleAttribute(true) public final class FieldBuilder extends FieldInfo implements _FieldBuilder
Not applicable.
Get an instance of FieldBuilder by calling DefineField, DefineInitializedData, or DefineUninitializedData.
The following code sample illustrates the use of FieldBuilder.
using System; using System.Threading; using System.Reflection; using System.Reflection.Emit; using System.Security.Permissions; public class FieldBuilder_Sample { private static Type CreateType(AppDomain currentDomain) { // Create an assembly. AssemblyName myAssemblyName = new AssemblyName(); myAssemblyName.Name = "DynamicAssembly"; AssemblyBuilder myAssembly = 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); Console.WriteLine("Name :"+myFieldBuilder.Name); Console.WriteLine("DeclaringType :"+myFieldBuilder.DeclaringType); Console.WriteLine("Type :"+myFieldBuilder.FieldType); Console.WriteLine("Token :"+myFieldBuilder.GetToken().Token); return myTypeBuilder.CreateType(); } [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")] public static void Main() { try { Type myType = CreateType(Thread.GetDomain()); // 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); Console.WriteLine("MyClass.MyMethod returned: \"" + myObject + "\""); } catch( Exception e) { Console.WriteLine("Exception Caught "+e.Message); } } }
System.Object
System.Reflection.MemberInfo
System.Reflection.FieldInfo
System.Reflection.Emit.FieldBuilder
System.Reflection.MemberInfo
System.Reflection.FieldInfo
System.Reflection.Emit.FieldBuilder
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show:
Note: