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.

Reflecting a Property

Obtaining property information using reflection is a similar process to accessing field data. If you have not read the article, "Reflecting Field Information", you should do so before continuing. As with fields, you can obtain information for public properties with simple calls and access the details of non-public members using a combination of binding flags. In this article we won't repeat the basics of member reflection. We will simply create a project that includes a class with several properties. We'll then reflect those properties and look at the information that is available.

To begin, create a console application and add the following using directive to simplify access to the Reflection classes:

using System.Reflection;

Add the following class to the project:

public class PropertyTest
{
    string _readWrite;
    string _readOnly;
    string _writeOnly;

    public string ReadWrite
    {
        get { return _readWrite; }
        set { _readWrite = value; }
    }

    public string ReadOnly
    {
        get { return _readOnly; }
    }

    private string WriteOnly
    {
        set { _writeOnly = value; }
    }
}

Obtaining Information for a Property

To reflect over a single property you can use the GetProperty method of the System.Type class. There are several overloaded versions of this method, including two that match the signatures of GetField. To obtain details of a public property you can call the method with a single string parameter containing the name of the property to locate. If you wish to reflect over non-public properties you can add a second argument containing a set of binding flags in a BindingFlags value.

The following code retrieves a PropertyInfo object for the public property, "ReadWrite":

Type type = typeof(PropertyTest);
PropertyInfo readWriteInfo = type.GetProperty("ReadWrite");
Console.WriteLine(readWriteInfo.Name);

// Outputs "ReadWrite"

The second example uses binding flags to obtain details for the private property, "WriteOnly".

Type type = typeof(PropertyTest);
PropertyInfo writeInfo = type.GetProperty(
    "WriteOnly", BindingFlags.Instance | BindingFlags.NonPublic);
Console.WriteLine(writeInfo.Name);

// Outputs "WriteOnly"

Obtaining Information for Multiple Properties

As with field reflection, you can obtain PropertyInfo objects for all of the properties in a class, or for a filtered list of those properties, with a single call. Calling GetProperties with no arguments returns a PropertyInfo array containing details of all of the public properties in a class or a structure. By adding a binding flags parameter you can decide to retrieve a filtered list of properties, specifying that you wish to query public members, non-public members or both, and filtering for static or instance members.

The code below returns all of the public properties:

Type type = typeof(PropertyTest);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
    Console.WriteLine(property.Name);
}

/* OUTPUT

ReadWrite
ReadOnly

*/

The next sample shows the use of binding flags to return details of non-public, instance properties:

Type type = typeof(PropertyTest);
PropertyInfo[] properties = type.GetProperties(
    BindingFlags.NonPublic | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
    Console.WriteLine(property.Name);
}

/* OUTPUT

WriteOnly

*/
25 February 2012