System.Reflection.FieldInfo Class

public abstract class FieldInfo : MemberInfo

Base Types

Object
  MemberInfo
    FieldInfo

Assembly

mscorlib

Library

Reflection

Summary

Provides access to field metadata.

See Also

System.Reflection Namespace

Members

FieldInfo Constructors

FieldInfo Constructor

FieldInfo Methods

FieldInfo.GetValue Method
FieldInfo.SetValue(System.Object, System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Globalization.CultureInfo) Method
FieldInfo.SetValue(System.Object, System.Object) Method

FieldInfo Properties

FieldInfo.Attributes Property
FieldInfo.FieldType Property


FieldInfo Constructor

protected FieldInfo();

Summary

Constructs a new instance of the FieldInfo class.

See Also

System.Reflection.FieldInfo Class, System.Reflection Namespace

FieldInfo.GetValue Method

public abstract object GetValue(object obj);

Summary

Obtains the value of the field that is reflected by the current instance and contained in the specified object.

Parameters

obj
An object that contains the field value to be returned. If the field reflected by the current instance is static, obj is ignored. For non-static fields, obj is required to be an instance of a class that inherits or declares the field.

Return Value

A Object that contains the value of the field reflected by the current instance.

Exceptions

Exception TypeCondition
NotSupportedExceptionA field is marked literal, but the field does not have one of the accepted literal types. [Note: For information regarding the accepted literal types, see Partition II of the CLI Specification.]

FieldAccessExceptionThe field reflected by the current instance is non-public, and the caller does not have permission to access non-public members.
ArgumentExceptionThe field reflected by the current instance is declared neither directly in obj nor in any class from which obj derives.
TargetExceptionThe field reflected by the current instance is non-static, and obj is null .

Description

[Behaviors: Before returning the value, the system checks to see if the user has access permission. ]

See Also

System.Reflection.FieldInfo Class, System.Reflection Namespace

FieldInfo.SetValue(System.Object, System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Globalization.CultureInfo) Method

public abstract void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);

Summary

Assigns the specified value to the field that is reflected by the current instance and contained in the specified object.

Parameters

obj
The object whose field value will be set. If the field is static, obj is ignored. For non-static fields, obj is required to be an instance of a class that inherits or declares the field.
value
An object that contains the value to assign to the field contained by obj .
invokeAttr
A BindingFlags value that controls the binding process.
binder
A Binder instance that enables the binding, coercion of argument types, and invocation of members through reflection. If binder is null , the default binder of the current implementation is used.
culture
The only defined value for this parameter is null .

Exceptions

Exception TypeCondition
ArgumentExceptionThe field reflected by the current instance is declared neither directly in obj nor in any class from which obj derives.

value is not assignment-compatible with the type of the field reflected by the current instance.

FieldAccessExceptionThe field reflected by the current instance is non-public, and the caller does not have permission to access non-public members.
TargetExceptionThe field reflected by the current instance is non-static, and obj is null .

Description

[Behaviors: Before setting the value, the system verifies that the user has access permission. ]

See Also

System.Reflection.FieldInfo Class, System.Reflection Namespace

FieldInfo.SetValue(System.Object, System.Object) Method

public void SetValue(object obj, object value);

Summary

Assigns the specified value to the field that is reflected by the current instance and contained in the specified object.

Parameters

obj
The object whose field value will be set. If the field is static, obj is ignored. For non-static fields, obj is required to be an instance of a class that inherits or declares the field.
value
A Object that contains the value to assign to the field contained by obj .

Exceptions

Exception TypeCondition
ArgumentExceptionThe field reflected by the current instance is declared neither directly in obj nor in any class from which obj derives.

value is not assignment-compatible with the type of the field reflected by the current instance.

FieldAccessExceptionThe field reflected by the current instance is non-public, and the caller does not have permission to access non-public members.
TargetExceptionThe field reflected by the current instance is non-static, and obj is null .

Description

Before setting the value, the system verifies that the user has access permission. If the user does not have access permission, a FieldAccessException is thrown.

See Also

System.Reflection.FieldInfo Class, System.Reflection Namespace

FieldInfo.Attributes Property

public abstract FieldAttributes Attributes { get; }

Summary

Gets the attributes of the field reflected by the current instance.

Property Value

A FieldAttributes value that indicates the attributes of the field reflected by the current instance.

Description

[Behaviors: This property is read-only.]

[Usage: Use this property to determine the accessibility of the field reflected by the current instance. Also use this property to determine if the field reflected by the current instance can be set after it is initialized, is implemented in native code, is a literal, or has a special name.]

Example

The following example demonstrates obtaining the attributes of two fields.

using System;
using System.Reflection;

class MyClass
{

   public int MyPublicInstanceField;
   private const int MyPrivateConstField = 10;

}

class FieldAttributesExample
{

   public static void Main()
   {

      Type t = (typeof(MyClass));
      string str;
      FieldInfo[] fiAry = t.GetFields( BindingFlags.Static |
         BindingFlags.Instance | BindingFlags.Public |
         BindingFlags.NonPublic | BindingFlags.DeclaredOnly );
      foreach (FieldInfo fi in fiAry)
      {
         Console.WriteLine("Field {0} is: ", fi.Name);
         str = ((fi.Attributes & FieldAttributes.Static) != 0) ?
            "Static" : "Instance";
         Console.Write(str + " ");
         str = ((fi.Attributes & FieldAttributes.Public) != 0) ?
            "Public" : "Not-Public";
         Console.Write(str + " ");
         str = ((fi.Attributes & FieldAttributes.Literal) != 0) ?
            "Literal" : String.Empty;
         Console.WriteLine(str);

      }

   }

}
      
The output is

Field MyPublicInstanceField is:

Instance Public

Field MyPrivateConstField is:

Static Not-Public Literal

See Also

System.Reflection.FieldInfo Class, System.Reflection Namespace

FieldInfo.FieldType Property

public abstract Type FieldType { get; }

Summary

Gets the type of the field reflected by the current instance.

Property Value

The Type of the field reflected by the current instance.

Description

This property is read-only.

See Also

System.Reflection.FieldInfo Class, System.Reflection Namespace