PropertyInfo.GetValue Method (Object, Object[])
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns the value of the property with optional index values for indexed properties.
Assembly: mscorlib (in mscorlib.dll)
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.
| 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 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 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 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.
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.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
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 */
Note: