System.Reflection.PropertyInfo.GetValue Method

Returns the property value of a specified object with optional index values for indexed properties.

Syntax

[System.Diagnostics.DebuggerStepThrough]
public virtual object GetValue (object obj, object[] index)

Parameters

index
Optional index values for indexed properties. This value should be null for non-indexed properties.
obj
The object whose property value will be returned.
index
Optional index values for indexed properties. This value should be null for non-indexed properties.

Returns

The property value of the specified object.

Exceptions

TypeReason
ArgumentException

index does not contain the exact type of arguments needed.

-or-

The Get accessor of the property reflected by the current instance is not found.

MethodAccessExceptionThe Get accessor of the property reflected by the current instance is non-public and the caller does not have System.Security.Permissions.ReflectionPermission to reflect on non-public methods.
System.Reflection.TargetExceptionThe property reflected by the current instance is non-static, and obj is null or is of a type that does not implement the property reflected by the current instance.
System.Reflection.TargetParameterCountExceptionThe current instance reflects an indexer and index.Length does not equal the rank of the indexer.

Remarks

To determine whether a property is indexed, use the PropertyInfo.GetIndexParameters method. If the resulting array has 0 (zero) elements, the property is not indexed.

This is a convenience method that provides an implementation for the abstract GetValue method with a BindingFlags parameter of Default, the Binder set to null, and the CultureInfo set to null.

Because static properties belong to the type, not individual objects, get static properties by passing null as the object argument. For example, use the following code to get the static CurrentCulture property of CultureInfo :

Example

PropertyInfo CurCultProp = 
    (typeof(CultureInfo)).GetProperty("CurrentCulture"); 
Console.WriteLine("CurrCult: " +
    CurCultProp.GetValue(null,null));

To use the GetValue method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetValue method.

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 reflect 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