Returns the value of the property with optional index values for indexed properties.
Namespace:
System.Reflection
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overridable Function GetValue ( _
obj As Object, _
index As Object() _
) As Object
Dim instance As PropertyInfo
Dim obj As Object
Dim index As Object()
Dim returnValue As Object
returnValue = instance.GetValue(obj, index)
public virtual Object GetValue(
Object obj,
Object[] index
)
public:
virtual Object^ GetValue(
Object^ obj,
array<Object^>^ index
)
public function GetValue(
obj : Object,
index : Object[]
) : Object
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 nullNothingnullptra null reference (Nothing in Visual Basic) for non-indexed properties.
Implements
_PropertyInfo..::.GetValue(Object, array<Object>[]()[])
| 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- The property is an instance property, but obj is nullNothingnullptra 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 | 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.
This is a convenience method that provides an implementation for the abstract GetValue method with a BindingFlags parameter of Default, the Binder set to nullNothingnullptra null reference (Nothing in Visual Basic), and the CultureInfo set to nullNothingnullptra null reference (Nothing in Visual Basic).
Because static properties belong to the type, not to individual objects, get static properties by passing nullNothingnullptra null reference (Nothing in Visual Basic) 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.
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
Imports System.Reflection
Module Example
Sub Main()
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 the index is zero-based.
Dim indexArgs() As Object = {6}
Dim value As Object = pinfo.GetValue(test, indexArgs)
Console.WriteLine(value)
' Show the complete string.
For x As Integer = 0 To test.Length - 1
Console.Write(pinfo.GetValue(test, New Object() {x}))
Next
Console.WriteLine()
End Sub
End Module
using System;
using System.Reflection;
class Example
{
public static void Main()
{
string test = "abcdefghijklmnopqrstuvwxyz";
// To retrieve the value of the indexed Chars property using
// reflection, first get a 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.
// Show the seventh letter (g)
object[] indexArgs = { 6 };
object value = pinfo.GetValue(test, indexArgs);
Console.WriteLine(value);
// Show the complete string.
for (int x = 0; x < test.Length; x++)
{
Console.Write(pinfo.GetValue(test, new Object[] {x}));
}
}
}
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
Reference