System.Reflection.FieldInfo.GetValue Method

When overridden in a derived class, returns the value of a field supported by a given object.

Syntax

public abstract object GetValue (object obj)

Parameters

obj
The object whose field value will be returned.

Returns

An object containing the value of the field reflected by this instance.

Exceptions

TypeReason
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.
System.Reflection.TargetExceptionThe field reflected by the current instance is non-static, and obj is null.

Remarks

If the field is static, obj is ignored. For non-static fields, obj should be an instance of a class that inherits or declares the field. Note that the return type of GetValue is Object. For example, if the field holds a Boolean primitive value, an instance of Object with the appropriate Boolean value is returned. Before returning the value, GetValue checks to see if the user has access permission.

Note:

Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked through reflection whenever the code is fully trusted.

Note:

Starting with the net_v20sp1_long, this method can be used to access non-public members if the caller has been granted System.Security.Permissions.ReflectionPermission with the System.Security.Permissions.ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public members is restricted to the caller’s grant set, or a subset thereof. (See Security Considerations for Reflection.)

To use this functionality, your application should target the net_v35_long or later.

Permissions

TypeReason
System.Security.Permissions.ReflectionPermissionRequires permission to access non-public members of a type in loaded assemblies. See System.Security.Permissions.ReflectionPermissionFlag.MemberAccess.

Requirements

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0