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

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

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

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

Syntax

'Declaration
<DebuggerStepThroughAttribute> _
<DebuggerHiddenAttribute> _
Public Overridable Function GetValue ( _
    obj As Object, _
    index As Object() _
) As Object
[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: array<System.Object[]
    Optional index values for indexed properties. This value should be nulla null reference (Nothing in Visual Basic) for non-indexed properties.

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.

This is a convenience method that provides an implementation for the abstract GetValue method with a BindingFlags parameter of Default, the Binder set to nulla null reference (Nothing in Visual Basic), and the CultureInfo set to nulla null reference (Nothing in Visual Basic).

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

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

Examples

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.

Imports System.Reflection

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 

        Dim test As String = "abcdefghijklmnopqrstuvwxyz"

        ' To retrieve the value of the indexed Chars property  using 
        ' reflection, create an instance of PropertyInfo for Chars.
        '
        Dim pinfo As PropertyInfo = GetType(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.
        Dim indexArgs() As Object = { 6 }
        Dim value As Object = pinfo.GetValue(test, indexArgs)
        outputBlock.Text &= _
            String.Format("The character at index 6 is ""{0}""." & vbLf, value)

        ' Show the complete string, one character at a time.
        For i As Integer = 0 To test.Length - 1
            outputBlock.Text &= pinfo.GetValue(test, New Object() { i })
        Next
        outputBlock.Text &= vbLf

    End Sub

End Class

' This example produces the following output:
'
'The character at index 6 is "g".
'abcdefghijklmnopqrstuvwxyz
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
 */

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.