다음을 통해 공유


FieldBuilder Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines and represents a field. This class cannot be inherited.

Inheritance Hierarchy

System.Object
  System.Reflection.MemberInfo
    System.Reflection.FieldInfo
      System.Reflection.Emit.FieldBuilder

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class FieldBuilder _
    Inherits FieldInfo
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public sealed class FieldBuilder : FieldInfo

The FieldBuilder type exposes the following members.

Properties

  Name Description
Public property Attributes Gets the attributes of this field. (Overrides FieldInfo.Attributes.)
Public property DeclaringType Gets a reference to the Type object for the type that declares this field. (Overrides MemberInfo.DeclaringType.)
Public property FieldHandle Gets the internal metadata handle for this field. (Overrides FieldInfo.FieldHandle.)
Public property FieldType Gets the Type object that represents the type of this field. (Overrides FieldInfo.FieldType.)
Public property 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.)
Public property 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.)
Public property 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.)
Public property 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.)
Public property IsInitOnly Gets a value that indicates whether the field can be set only in the body of the constructor. (Inherited from FieldInfo.)
Public property IsLiteral Gets a value that indicates whether the value is written at compile time and cannot be changed. (Inherited from FieldInfo.)
Public property IsNotSerialized Gets a value that indicates whether this field has the NotSerialized attribute. (Inherited from FieldInfo.)
Public property IsPinvokeImpl Gets a value that indicates whether the corresponding PinvokeImpl attribute is set in FieldAttributes. (Inherited from FieldInfo.)
Public property IsPrivate Gets a value that indicates whether the field is private. (Inherited from FieldInfo.)
Public property IsPublic Gets a value that indicates whether the field is public. (Inherited from FieldInfo.)
Public property IsSpecialName Gets a value that indicates whether the field has a name that has special significance. (Inherited from FieldInfo.)
Public property IsStatic Gets a value that indicates whether the field is static (Shared in Visual Basic). (Inherited from FieldInfo.)
Public property MemberType Gets a value that indicates that this member is a field. (Inherited from FieldInfo.)
Public property MetadataToken Gets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public property Module Gets the module in which the type that contains this field is being defined. (Overrides MemberInfo.Module.)
Public property Name Gets the name of this field. (Overrides MemberInfo.Name.)
Public property ReflectedType Gets the reference to the Type object from which this object was obtained. (Overrides MemberInfo.ReflectedType.)

Top

Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method 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.)
Public method GetCustomAttributes(Boolean) Returns all the custom attributes defined for this field. (Overrides MemberInfo.GetCustomAttributes(Boolean).)
Public method GetCustomAttributes(Type, Boolean) Returns all the custom attributes defined for this field identified by the given type. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetRawConstantValue Returns a literal value associated with the field by a compiler. (Inherited from FieldInfo.)
Public method GetToken Returns the token representing this field.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValue Retrieves the value of the field supported by the given object. (Overrides FieldInfo.GetValue(Object).)
Public method IsDefined Gets a value that indicates whether an attribute having the specified type is defined on a field. (Overrides MemberInfo.IsDefined(Type, Boolean).)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetConstant Sets the default value of this field.
Public method SetCustomAttribute Sets a custom attribute using a custom attribute builder.
Public method SetValue(Object, Object) Sets the value of the field that is supported by the given object. (Inherited from FieldInfo.)
Public method 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).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

Get an instance of FieldBuilder by calling the TypeBuilder.DefineField method.

NoteNote:

The SetValue method is currently not supported. As a workaround, retrieve the FieldInfo by reflecting on the finished type and call SetValue to set the value of the field.

Examples

The following code sample illustrates the use of FieldBuilder.

Imports System.Reflection
Imports System.Reflection.Emit

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Create an assembly.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "DynamicAssembly"
      Dim myAssembly As AssemblyBuilder = _
         AppDomain.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)
      outputBlock.Text &= "Name: " & myFieldBuilder.Name & vbCrLf
      outputBlock.Text &= "DeclaringType: " & myFieldBuilder.DeclaringType.ToString() & vbCrLf
      outputBlock.Text &= "Type: " & myFieldBuilder.FieldType.ToString() & vbCrLf
      outputBlock.Text &= "Token: " & myFieldBuilder.GetToken().Token.ToString() & vbCrLf

      Dim myType As Type = myTypeBuilder.CreateType()
      ' 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)
      outputBlock.Text &= "MyClass.MyMethod returned: """ & myObject & """" & vbCrLf

   End Sub 
End Class 
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";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.