FieldBuilder Class
Defines and represents a field. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
Note: |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
Get an instance of FieldBuilder by calling DefineField, DefineInitializedData, or DefineUninitializedData.
The following code sample illustrates the use of FieldBuilder.
Imports System Imports System.Threading Imports System.Reflection Imports System.Reflection.Emit Imports System.Security.Permissions Public Class FieldBuilder_Sample Private Shared Function CreateType(currentDomain As AppDomain) As Type ' Create an assembly. Dim myAssemblyName As New AssemblyName() myAssemblyName.Name = "DynamicAssembly" Dim myAssembly As AssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _ AssemblyBuilderAccess.Run) ' Create a dynamic module in Dynamic Assembly. Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("MyModule") ' Define a public class named "MyClass" in the assembly. Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType("MyClass", _ TypeAttributes.Public) ' Define a private String field named "MyField" in the type. Dim myFieldBuilder As FieldBuilder = myTypeBuilder.DefineField("MyField", _ GetType(String), FieldAttributes.Private Or FieldAttributes.Static) ' Create the constructor. Dim constructorArgs As Type() = {GetType(String)} Dim constructor As ConstructorBuilder = _ myTypeBuilder.DefineConstructor(MethodAttributes.Public, _ CallingConventions.Standard, constructorArgs) Dim constructorIL As ILGenerator = constructor.GetILGenerator() constructorIL.Emit(OpCodes.Ldarg_0) Dim superConstructor As ConstructorInfo = GetType(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. Dim myMethodBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod", _ MethodAttributes.Public, GetType(String), Nothing) Dim methodIL As ILGenerator = 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.ToString()) Console.WriteLine("Type :" + myFieldBuilder.FieldType.ToString()) Console.WriteLine("Token :" + myFieldBuilder.GetToken().Token.ToString()) Return myTypeBuilder.CreateType() End Function 'CreateType <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _ Public Shared Sub Main() Try Dim myType As Type = CreateType(Thread.GetDomain()) ' Create an instance of the "HelloWorld" class. Dim helloWorld As Object = Activator.CreateInstance(myType, New Object() {"HelloWorld"}) ' Invoke the "MyMethod" method of the "MyClass" class. Dim myObject As Object = myType.InvokeMember("MyMethod", _ BindingFlags.InvokeMethod, Nothing, helloWorld, Nothing) Console.WriteLine("MyClass.MyMethod returned: " & Microsoft.VisualBasic.Chr(34) & myObject & Microsoft.VisualBasic.Chr(34) ) Catch e as Exception Console.WriteLine("Exception Caught "+e.Message) End Try End Sub 'Main End Class 'FieldBuilder_Sample
System.Reflection.MemberInfo
System.Reflection.FieldInfo
System.Reflection.Emit.FieldBuilder
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: