public abstract class FieldInfo : MemberInfo
Object
MemberInfo
FieldInfo
mscorlib
Reflection
Provides access to field metadata.
System.Reflection Namespace
FieldInfo Constructors
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
protected FieldInfo();
Constructs a new instance of the FieldInfo class.
System.Reflection.FieldInfo Class, System.Reflection Namespace
public abstract object GetValue(object obj);
Obtains the value of the field that is reflected by the current instance and contained in the specified object.
- 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.
A Object that contains the value of the field reflected by the current instance.
Exception Type Condition NotSupportedException A 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.]
FieldAccessException The field reflected by the current instance is non-public, and the caller does not have permission to access non-public members. ArgumentException The field reflected by the current instance is declared neither directly in obj nor in any class from which obj derives. TargetException The field reflected by the current instance is non-static, and obj is null
.
[Behaviors: Before returning the value, the system checks to see if the user has access permission. ]
System.Reflection.FieldInfo Class, System.Reflection Namespace
public abstract void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
Assigns the specified value to the field that is reflected by the current instance and contained in the specified object.
- 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
.
Exception Type Condition ArgumentException The 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.
FieldAccessException The field reflected by the current instance is non-public, and the caller does not have permission to access non-public members. TargetException The field reflected by the current instance is non-static, and obj is null
.
[Behaviors: Before setting the value, the system verifies that the user has access permission. ]
System.Reflection.FieldInfo Class, System.Reflection Namespace
public void SetValue(object obj, object value);
Assigns the specified value to the field that is reflected by the current instance and contained in the specified object.
- 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 .
Exception Type Condition ArgumentException The 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.
FieldAccessException The field reflected by the current instance is non-public, and the caller does not have permission to access non-public members. TargetException The field reflected by the current instance is non-static, and obj is null
.
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.
System.Reflection.FieldInfo Class, System.Reflection Namespace
public abstract FieldAttributes Attributes { get; }
Gets the attributes of the field reflected by the current instance.
A FieldAttributes value that indicates the attributes of the field reflected by the current instance.
[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.]
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
System.Reflection.FieldInfo Class, System.Reflection Namespace
public abstract Type FieldType { get; }
Gets the type of the field reflected by the current instance.
The Type of the field reflected by the current instance.
This property is read-only.
System.Reflection.FieldInfo Class, System.Reflection Namespace