Export (0) Print
Expand All

PropertyInfo.GetValue Method (Object, Object[])

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

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

[DebuggerStepThroughAttribute]
[DebuggerHiddenAttribute]
public virtual Object GetValue(
	Object obj,
	Object[] index
)

Parameters

obj
Type: System.Object
The object whose property value will be returned.
index
Type: System.Object[]
Optional index values for indexed properties. This value should be null for non-indexed properties.

Return Value

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

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

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.

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.

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 to 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:

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 an ArgumentNullException instead of TargetException.

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

The following example shows how to get the value of an indexed property. The String.Chars property is the default property (the indexer in C#) of the String class.


using System;
using System.Reflection;

class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        string test = "abcdefghijklmnopqrstuvwxyz";

        // To retrieve the value of the indexed Chars property  using 
        // reflection, create an instance of PropertyInfo for Chars.
        //
        PropertyInfo pinfo = typeof(string).GetProperty("Chars");

        // To retrieve an instance property, the GetValue method
        // requires the object whose property is being accessed, and 
        // an array of objects representing the index values. 

        // Get the seventh character in the test string. 
        // Note that the index is zero-based.
        object[] indexArgs = { 6 };
        object value = pinfo.GetValue(test, indexArgs);
        outputBlock.Text += 
            String.Format("The character at index 6 is \"{0}\".\n", value);

        // Show the complete string, one character at a time.
        for(int i = 0; i < test.Length; i++)
        {
            outputBlock.Text += pinfo.GetValue(test, new object[] { i });
        }
        outputBlock.Text += "\n";
    }
}

/* This example produces the following output:

The character at index 6 is "g".
abcdefghijklmnopqrstuvwxyz
 */


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

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

Community Additions

ADD
Show:
© 2015 Microsoft