Mono Class Library: System.Reflection.MemberInfo Overview | Members

System.Reflection.MemberInfo.DeclaringType Property

Gets the type that declares the member reflected by the current instance. [Edit]

public abstract Type DeclaringType { get; }

Value

The Type object of the class that declares the member reflected by the current instance; or, null if the member reflected by the current instance is a global member. [Edit]

Remarks

Note:

A member of a class (or interface) is either declared on that type or inherited from a base class (or interface). The MemberInfo.DeclaringType property value cannot be the same as the Type object used to obtain the current instance. These values will differ if either of the following conditions is true.

  • If the Type object from which the current instance was obtained did not declare the member reflected by the current instance, the MemberInfo.DeclaringType will represent the base type that is closest to that object in its hierarchy chain and declares the member reflected by the current instance.
  • If the current instance reflects a global member, (that is, it was obtained from Module.GetMethods , which returns global methods on a module), then the MemberInfo.DeclaringType property value is null .
Operation

This property is read-only.

This property is required to return the Type object for the type that declares the member reflected by the current instance. This property value is required to be equal to the MemberInfo.ReflectedType property value of the current instance if and only if the reflected type also contains a declaration for the member reflected by the current instance.

Note to Inheritors
Override this property to get the Type of the class that declared the member that is reflected by the current instance.

Usage
Use this property to determine the Type of the class that declared the member that is reflected by the current instance.

[Edit]

Example

The following example demonstrates the difference between the MemberInfo.DeclaringType and MemberInfo.ReflectedType of a member.

C# Example
using System;
using System.Reflection;

public class BaseClass {
   public void ReflectedMethod() {}
}

public class DerivedClass: BaseClass {}

public class DeclaringTypeExample {
   public static void Main() {
    Type t = typeof(DerivedClass);
    MemberInfo [] memInfo = t.GetMember("ReflectedMethod");
    Console.WriteLine("Reflected type is {0}.", memInfo[0].ReflectedType);
    Console.WriteLine("Declaring type is {0}.", memInfo[0].DeclaringType);    
   }
}
   

The output is

Reflected type is DerivedClass.
Declaring type is BaseClass.

Requirements

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0