Export (0) Print
Expand All

PropertyInfo.GetValue Method (Object, BindingFlags, Binder, Object[], CultureInfo)

When overridden in a derived class, returns the value of a property that has the specified binding, index, and CultureInfo.

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

public abstract Object GetValue(
	Object obj,
	BindingFlags invokeAttr,
	Binder binder,
	Object[] index,
	CultureInfo culture
)

Parameters

obj
Type: System.Object

The object whose property value will be returned.

invokeAttr
Type: System.Reflection.BindingFlags

The invocation attribute. This must be a bit flag from BindingFlags: InvokeMethod, CreateInstance, Static, GetField, SetField, GetProperty, or SetProperty. A suitable invocation attribute must be specified. If a static member is to be invoked, the Static flag of BindingFlags must be set.

binder
Type: System.Reflection.Binder

An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects via reflection. If binder is null, the default binder is used.

index
Type: System.Object[]

Optional index values for indexed properties. This value should be null for non-indexed properties.

culture
Type: System.Globalization.CultureInfo

The culture for which the resource is to be localized. Note that if the resource is not localized for this culture, the CultureInfo.Parent method will be called successively in search of a match. If this value is null, the CultureInfo is obtained from the CultureInfo.CurrentUICulture property.

Return Value

Type: System.Object
The property value for obj.

Implements

_PropertyInfo.GetValue(Object, BindingFlags, Binder, Object[], CultureInfo)

ExceptionCondition
ArgumentException

The index array does not contain the type of arguments needed.

-or-

The property's get accessor is not found.

TargetException

The object does not match the target type.

-or-

The property is an instance property, but obj is null.

TargetParameterCountException

The number of parameters in index does not match the number of parameters the indexed property takes.

MethodAccessException

There was an illegal attempt to access a private or protected method inside a class.

TargetInvocationException

An error occurred while retrieving the property value. For example, an index value specified for an indexed property is out of range. The InnerException property indicates the reason for the error.

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

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

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.

NoteNote:

Starting with the .NET Framework version 2.0 Service Pack 1, this method can be used to access non-public members if the caller has been granted ReflectionPermission with the 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 Framework version 3.5. For more information, see .NET Framework 3.5 Architecture.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft