BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Reflection
.NET 1.1+

Reflecting Property Information

The fourth part of the Reflection tutorial describes the reflection of properties for classes and structures. A type's property information can be obtained as PropertyInfo objects, using methods of the System.Type class.

Common PropertyInfo Properties

Once you have obtained a PropertyInfo instance you can examine its properties to find out more about the property it relates to. The remaining two sections describe the common properties.

MemberInfo Properties

The PropertyInfo class is a subclass of MemberInfo. MemberInfo is also the base class for the reflection classes of other member types, including that of the FieldInfo class we saw in the previous instalment of the tutorial. MemberInfo defines several useful properties, listed below:

  • Name. Returns the name of the member as a string.
  • MemberType. Returns a value from the MemberTypes enumeration, which specifies the type of a member. This enumeration includes values for each type of member that can be present in a class. When reading properties, the value will always be Property.
  • DeclaringType. Returns a System.Type object that describes the class or structure in which the property is declared. For our test code, this is the PropertyTest class.
  • ReflectedType. Returns a System.Type object that describes the class or structure that was reflected over to obtain the member data. In the sample code below, this is the PropertyTest class.
Type type = typeof(PropertyTest);
PropertyInfo property = type.GetProperty("ReadWrite");
Console.WriteLine("Name:\t\t{0}",property.Name);
Console.WriteLine("MemberType:\t{0}", property.MemberType);
Console.WriteLine("DeclaringType:\t{0}", property.DeclaringType.Name);
Console.WriteLine("ReflectedType:\t{0}", property.ReflectedType.Name);

/* OUTPUT

Name:           ReadWrite
MemberType:     Property
DeclaringType:  PropertyTest
ReflectedType:  PropertyTest

*/

PropertyInfo Properties

PropertyInfo adds extra properties to that provided by MemberInfo. These include:

  • CanRead. Returns a Boolean value that indicates whether the value of the related member can be read. This is true for read/write and read-only properties and false for write-only properties.
  • CanWrite. Returns a Boolean value that indicates whether the property value can be set. This is true for read/write and write-only properties and false for read-only members.
  • PropertyType. Returns a System.Type object that represents the data type of the property. For the three properties in the sample class, this property returns a Type instance that describes the System.String type.
Type type = typeof(PropertyTest);
PropertyInfo property = type.GetProperty("ReadOnly");
Console.WriteLine("CanRead:\t{0}", property.CanRead);
Console.WriteLine("CanWrite:\t{0}", property.CanWrite);
Console.WriteLine("PropertyType:\t{0}", property.PropertyType);

/* OUTPUT

CanRead:        True
CanWrite:       False
PropertyType:   System.String

*/
25 February 2012