MemberInfo.DeclaringType Property

Definition

Gets the class that declares this member.

public abstract Type DeclaringType { get; }
public abstract Type? DeclaringType { get; }

Property Value

The Type object for the class that declares this member.

Implements

Examples

The following example defines an interface, IValue, with a single member, GetValue. It also defines four classes: A, a base class that implements the IValue interface; B, which inherits from A and hides its implementation of GetValue from the base class implementation; C, which simply inherits from A; and D, which inherits from A and overrides its GetValue method. The example then retrieves a MemberInfo object for each member of the type (including members inherited from Object) and displays the value of its DeclaringType property.

using System;
using System.Reflection;

interface IValue
{
   int GetValue() ;
};

class A : IValue
{
   public virtual int GetValue()
   {
      return 0;
   }
};

class B : A
{
   public new int GetValue()
   {
      return 0;
   }
};

class C : A
{ };

class D : A
{
   public override int GetValue()
   {
      return 0;
   }
}

public class Example
{
   public static void Main()
   {
      // Get members of IValue interface.
      ShowMembersAndDeclaringTypes(typeof(IValue));
      Console.WriteLine();
      ShowMembersAndDeclaringTypes(typeof(A));
      Console.WriteLine();
      ShowMembersAndDeclaringTypes(typeof(B));
      Console.WriteLine();
      ShowMembersAndDeclaringTypes(typeof(C));
      Console.WriteLine();
      ShowMembersAndDeclaringTypes(typeof(D));
      Console.WriteLine();
   }

   private static void ShowMembersAndDeclaringTypes(Type t)
   {
      MemberInfo[] members = t.GetMembers();
      Console.WriteLine("{0} Members: ", t.Name);
      foreach (var member in members)
         Console.WriteLine("   {0}, Declaring type: {1}",
                           member.Name, member.DeclaringType.Name);
   }
}
// The example displays the following output:
//       IValue Members:
//          GetValue, Declaring type: IValue
//
//       A Members:
//          GetValue, Declaring type: A
//          ToString, Declaring type: Object
//          Equals, Declaring type: Object
//          GetHashCode, Declaring type: Object
//          GetType, Declaring type: Object
//          .ctor, Declaring type: A
//
//       B Members:
//          GetValue, Declaring type: B
//          GetValue, Declaring type: A
//          ToString, Declaring type: Object
//          Equals, Declaring type: Object
//          GetHashCode, Declaring type: Object
//          GetType, Declaring type: Object
//          .ctor, Declaring type: B
//
//       C Members:
//          GetValue, Declaring type: A
//          ToString, Declaring type: Object
//          Equals, Declaring type: Object
//          GetHashCode, Declaring type: Object
//          GetType, Declaring type: Object
//          .ctor, Declaring type: C
//
//       D Members:
//          GetValue, Declaring type: D
//          ToString, Declaring type: Object
//          Equals, Declaring type: Object
//          GetHashCode, Declaring type: Object
//          GetType, Declaring type: Object
//          .ctor, Declaring type: D

Note that the declaring type of A.GetValue is A, that B includes two GetValue methods, one declared by A and one by B, and that the declaring type of D.GetValue is D.

Note

DeclaringType returns only the member names and the names of their declaring types. To return the member names with their prototypes, call MemberInfo.ToString.

Remarks

The DeclaringType property retrieves a reference to the Type object for the type that declares this member. A member of a type is either declared by the type or inherited from a base type, so the Type object returned by the DeclaringType property might not be the same as the Type object used to obtain the current MemberInfo object.

  • If the Type object from which this MemberInfo object was obtained did not declare this member, the DeclaringType property will represent one of its base types.

  • If the MemberInfo object is a global member (that is, if it was obtained from the Module.GetMethods method, which returns global methods on a module), the returned DeclaringType will be null.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0