EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

PropertyInfo.GetValue Method (Object, Object())

May 11, 2014

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

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

'Déclaration
Public Overridable Function GetValue ( _
	obj As Object, _
	index As Object() _
) As Object

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 Nothing 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 Nothing.

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 Windows Phone, 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 Nothing, and the CultureInfo set to Nothing.

Because static properties belong to the type, not to individual objects, get static properties by passing Nothing 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.

Version Notes

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.


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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Afficher:
© 2014 Microsoft