Export (0) Print
Expand All

FieldInfo Class

Discovers the attributes of a field and provides access to field metadata.

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

[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public abstract class FieldInfo : MemberInfo

The FieldInfo type exposes the following members.

  NameDescription
Protected methodFieldInfoInitializes a new instance of the FieldInfo class.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360AttributesGets the attributes that are associated with this field.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360DeclaringTypeGets the class that declares this member. (Inherited from MemberInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360FieldHandleGets a handle to the internal metadata representation of a field.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360FieldTypeGets the type of this field object.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsAssemblyGets 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.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsFamilyGets 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.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsFamilyAndAssemblyGets 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.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsFamilyOrAssemblyGets 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.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsInitOnlyGets a value that indicates whether the field can be set only in the body of the constructor.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsLiteralGets a value that indicates whether the value is written at compile time and cannot be changed.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsNotSerializedGets a value that indicates whether this field has the NotSerialized attribute.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsPinvokeImplGets a value that indicates whether the corresponding PinvokeImpl attribute is set in FieldAttributes.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsPrivateGets a value that indicates whether the field is private.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsPublicGets a value that indicates whether the field is public.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsSpecialNameGets a value that indicates whether the field has a name that has special significance.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsStaticGets a value that indicates whether the field is static (Shared in Visual Basic).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360MemberTypeGets a value that indicates that this member is a field. (Overrides MemberInfo.MemberType.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360MetadataTokenGets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360ModuleGets the module in which the type that declares the member represented by the current MemberInfo is defined. (Inherited from MemberInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NameGets the name of the current member. (Inherited from MemberInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360ReflectedTypeGets the class object that was used to obtain this instance of MemberInfo. (Inherited from MemberInfo.)
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Boolean)When overridden in a derived class, returns an array of all custom attributes applied to this member. (Inherited from MemberInfo.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Type, Boolean)When overridden in a derived class, returns an array of custom attributes applied to this member and identified by Type. (Inherited from MemberInfo.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetFieldFromHandle(RuntimeFieldHandle)Gets a FieldInfo for the field represented by the specified handle.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)Gets a FieldInfo for the field represented by the specified handle, for the specified generic type.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetRawConstantValueReturns a literal value associated with the field by a compiler.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetValueWhen overridden in a derived class, returns the value of a field supported by a given object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefinedWhen overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member. (Inherited from MemberInfo.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360SetValue(Object, Object)Sets the value of the field that is supported by the given object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360SetValue(Object, Object, BindingFlags, Binder, CultureInfo)When overridden in a derived class, sets the value of the field with the specified constraints on type conversion.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The FieldInfo class does not have a public constructor. FieldInfo objects are obtained by calling either Type.GetFields or Type.GetField.

Fields are variables that are defined in the class. FieldInfo provides access to the metadata for a field in a class and also enables late-bound access to the value of a field. In Silverlight, you can get the metadata of any field, regardless of its access level, but you cannot use FieldInfo to bypass access-level protection when you get or set the value of a field.

The following example uses the Type.GetFields method to get the fields of the Example class, and then displays information about the fields.


using System;
using System.Reflection;

public class Example
{
   public int myField1 = 0;
   protected string myField2 = null;
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      FieldInfo[] myFieldInfo;
      Type myType = typeof(Example);
      // Get the type and fields of Example.
      myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
          | BindingFlags.Public);
      outputBlock.Text += "\nThe fields of " +
          "Example are \n" + "\n";
      // Display the field information of Example.
      for (int i = 0; i < myFieldInfo.Length; i++)
      {
         outputBlock.Text += String.Format("\nName            : {0}", myFieldInfo[i].Name) + "\n";
         outputBlock.Text += String.Format("Declaring Type  : {0}", myFieldInfo[i].DeclaringType) + "\n";
         outputBlock.Text += String.Format("IsPublic        : {0}", myFieldInfo[i].IsPublic) + "\n";
         outputBlock.Text += String.Format("MemberType      : {0}", myFieldInfo[i].MemberType) + "\n";
         outputBlock.Text += String.Format("FieldType       : {0}", myFieldInfo[i].FieldType) + "\n";
         outputBlock.Text += String.Format("IsFamily        : {0}", myFieldInfo[i].IsFamily) + "\n";
      }
   }
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

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

This type is thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft