PropertyInfo.GetValue Method (Object)

.NET Framework (current version)

Returns the property value of a specified object.

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

public object GetValue(
	object 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);

   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}):", 
      foreach (var prop in props)
         if (prop.GetIndexParameters().Length == 0)
            Console.WriteLine("   {0} ({1}): {2}", prop.Name,
            Console.WriteLine("   {0} ({1}): <Indexed>", prop.Name,

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top