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 All Type Members

The eighth part of the Reflection tutorial continues examining how a type's members may be interrogated using reflection. This article looks at how all of the members of a class or structure, regardless of their types, can be examined.

Reflecting Member Information

The previous five articles in the reflection tutorial have concentrated on reflecting specific member types of classes and structures. In some situations you will not wish to obtain details about a specific type of member, instead preferring to examine members of any type that fulfil given criteria. There are methods within the System.Type class that permit this type of reflection, returning arrays of MemberInfo objects.

The MemberInfo class' properties have been described earlier in the article, "Reflecting Property Information", so they will not be listed again here. To demonstrate the use of the member reflection methods we will rely on two properties. These are the Name property, which returns the name of a member as a string, and MemberType, which gives you the type of member as a MemberTypes value.

The example code in this article uses types from the System.Reflection namespace, so ensure that you add the following using directive to your code files:

using System.Reflection;

We also need a test class to reflect over. We'll use the code shown below:

public class MemberTest
{
    private string _testProperty;

    public MemberTest(string initalProperty)
    {
        TestProperty = initalProperty;
    }
        
    public string TestProperty
    {
        get { return _testProperty; }
        set { _testProperty = value; }
    }

    public string TestMethod()
    {
        return TestMethodImpl();
    }

    internal string TestMethodImpl()
    {
        return "Hello, World";
    }
}

Obtaining Information for Named Members

If you have been following the tutorial so far, you might expect that you can call a method, providing a member name, and retrieve the public member with that name, should it exist. The System.Type class does have such a method but it returns an array of MemberInfo objects, rather than a single result. When you perform the reflection operation, all members that have the provided name are retrieved. If no matching members exist, the resultant array is empty.

You can see the use of the basic overloaded version of GetMember in the following sample code. Here one MemberInfo object is returned, confirmed by the value in the array's Length property. When we check the MemberType property we can see that TestProperty is a property.

Type type = typeof(MemberTest);
MemberInfo[] members = type.GetMember("TestProperty");
Console.WriteLine("{0} member(s)", members.Length);
Console.WriteLine("{0} - {1}", members[0].MemberType, members[0].Name);

/* OUTPUT

1 member(s)
Property - TestProperty

*/

If you wish to reflect over non-public members or if you wish to filter the retrieved members according to whether they are static or not, you can apply binding flags using the second parameter of GetMember. For example, the following code retrieves non-public, instance members with the name, TestMethodImpl".

Type type = typeof(MemberTest);
MemberInfo[] members = type.GetMember(
    "TestMethodImpl", BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine("{0} member(s)", members.Length);
Console.WriteLine("{0} - {1}", members[0].MemberType, members[0].Name);

/* OUTPUT

1 member(s)
Method - TestMethodImpl

*/

A third overloaded version of GetMember allows you to filter the results according to the types of member that you wish to obtain. In this overload the first argument specifies the name to look for, the third parameter receives binding flags and the additional, second argument is used to specify the types of member using a MemberTypes value. The MemberTypes enumeration includes the Flags attribute, permitting you to combine several constants using the bitwise OR operator when you wish to search for multiple member types.

The sample code below finds public instance methods named, "TestMethod".

Type type = typeof(MemberTest);
MemberInfo[] members = type.GetMember(
    "TestMethod", MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("{0} member(s)", members.Length);
Console.WriteLine("{0} - {1}", members[0].MemberType, members[0].Name);

/* OUTPUT

1 member(s)
Method - TestMethod

*/
6 April 2012