共用方式為


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public MustOverride Function GetValue ( _
    obj As Object, _
    invokeAttr As BindingFlags, _
    binder As Binder, _
    index As Object(), _
    culture As CultureInfo _
) As Object
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 nulla null reference (Nothing in Visual Basic), the default binder is used.
  • index
    Type: array<System.Object[]
    Optional index values for indexed properties. This value should be nulla null reference (Nothing in Visual Basic) 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 nulla null reference (Nothing in Visual Basic), the CultureInfo is obtained from the CultureInfo.CurrentUICulture property.

Return Value

Type: System.Object
The property value for the object specified by the obj parameter.

Exceptions

Exception Condition
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 a property is an instance property but obj is nulla null reference (Nothing in Visual Basic).

TargetParameterCountException

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

MethodAccessException

The property is not accessible to the caller.

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.

Remarks

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

In Silverlight, only accessible properties can be retrieved using reflection.

Because static properties belong to the type, not to individual objects, get static properties by passing nulla null reference (Nothing in Visual Basic) as the object argument. For example, use the following code to get the static CurrentCulture property of CultureInfo:

C#
object o = typeof(CultureInfo)
    .GetProperty("CurrentCulture").GetValue(null, null); 

Visual Basic
Dim o As Object = GetType(CultureInfo) _ 
    .GetProperty("CurrentCulture").GetValue(Nothing, Nothing))

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

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 If you pass invalid values to GetValue, the method throws ArgumentNullException instead of TargetException.

If the count of indexes is less than the real count, GetValue throws ArgumentException instead of TargetParameterCountException.

GetValue(Object, BindingFlags, Binder, array<Object[], CultureInfo) throws ArgumentException when the index parameter is a string value and binder is not the default binder.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.