Export (0) Print
Expand All

PropertyInfo.GetValue Method (Object)

.NET Framework 4.6 and 4.5

Returns the property value of a specified object.

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

public Object GetValue(
	Object obj
)

Parameters

obj
Type: System.Object

The object whose property value will be returned.

Return Value

Type: System.Object
The property value of the specified object.

You call the GetValue(Object) overload to retrieve the value of a non-indexed property; if you try to retrieve the value of an indexed property, the method throws a TargetParameterCountException exception. You can determine whether a property is indexed or not by calling the GetIndexParameters method. If the length of the returned ParameterInfo array is zero, the property is not indexed.

This is a convenience method that provides an implementation for the abstract GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) method with the BindingFlags parameter set to BindingFlags.Default, the Binder set to null, the object array of index values set to null, and the CultureInfo set to null.

The following example defines a Planet class that has two properties: Name, the name of the planet; and Distance, the planet's distance from Earth. The example instantiates a Planet object that represents the planet Jupiter and passes it to a GetPropertyValues method that displays information about the properties and uses the GetValue method to get the value of each Planet property.

using System;
using System.Reflection;

public class Planet
{
   private String planetName;
   private Double distanceFromEarth;

   public Planet(String name, Double distance)
   {
      planetName = name;
      distanceFromEarth = distance;
   } 

   public String Name
   { get { return planetName; } }

   public Double Distance 
   { get { return distanceFromEarth; }
     set { distanceFromEarth = value; } }
}

public class Example
{
   public static void Main()
   {
      Planet jupiter = new Planet("Jupiter", 3.65e08);
      GetPropertyValues(jupiter);
   }

   private static void GetPropertyValues(Object obj)
   {
      Type t = obj.GetType();
      Console.WriteLine("Type is: {0}", t.Name);
      PropertyInfo[] props = t.GetProperties();
      Console.WriteLine("Properties (N = {0}):", 
                        props.Length);
      foreach (var prop in props)
         if (prop.GetIndexParameters().Length == 0)
            Console.WriteLine("   {0} ({1}): {2}", prop.Name,
                              prop.PropertyType.Name,
                              prop.GetValue(obj));
         else
            Console.WriteLine("   {0} ({1}): <Indexed>", prop.Name,
                              prop.PropertyType.Name);

   }
}
// The example displays the following output: 
//       Type is: Planet 
//       Properties (N = 2): 
//          Name (String): Jupiter 
//          Distance (Double): 365000000

.NET Framework

Supported in: 4.6, 4.5

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft