Skip to main content
.NET Framework Class Library
MemberInfo..::.DeclaringType Property

Gets the class that declares this member.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)
Syntax
Public MustOverride ReadOnly Property DeclaringType As Type
	Get
public abstract Type DeclaringType { get; }
public:
virtual property Type^ DeclaringType {
	Type^ get () abstract;
}
abstract DeclaringType : Type

Property Value

Type: System..::.Type
The Type object for the class that declares this member.

Implements

_MemberInfo..::.DeclaringType
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 nullNothingnullptra null reference (Nothing in Visual Basic).

Examples

The following example shows how DeclaringType works with classes and interfaces and retrieves the member names of the System.IO.BufferedStream class, along with the class in which those members are declared. Also note that when B overrides virtual method M from A, it essentially redefines (or redeclares) this method. Therefore, B.M's MethodInfo reports the declaring type as B rather than A, even though A is where this method was originally declared.


Imports System
Imports System.IO
Imports System.Reflection
Imports Microsoft.VisualBasic

Namespace MyNamespace1

    Interface i
        Function MyVar() As Integer
    End Interface
    ' DeclaringType for MyVar is i.

    Class A
        Implements i
        Function MyVar() As Integer Implements i.MyVar
            Return 0
        End Function
    End Class
    ' DeclaringType for MyVar is A.

    Class B
        Inherits A
        Function MyVars() As Integer
            Return 0
        End Function
    End Class
    ' DeclaringType for MyVar is B.

    Class C
        Inherits A
    End Class
    ' DeclaringType for MyVar is A.
End Namespace

Namespace MyNamespace2

    Class A
        Public Overridable Sub M()
        End Sub
    End Class

    Class B
        Inherits A
        Public Overrides Sub M()
        End Sub
    End Class
End Namespace
Class Mymemberinfo

    Public Shared Sub Main()

        Console.WriteLine(ControlChars.Cr & "Reflection.MemberInfo")

        'Get the Type and MemberInfo.
        Dim MyType As Type = Type.GetType("System.IO.BufferedStream")

        Dim Mymemberinfoarray As MemberInfo() = MyType.GetMembers()

        'Get and display the DeclaringType method.
        Console.WriteLine(ControlChars.Cr & "There are {0} members in {1}.", Mymemberinfoarray.Length, MyType.FullName)

        Dim Mymemberinfo As MemberInfo
        For Each Mymemberinfo In Mymemberinfoarray
            Console.WriteLine("The declaring type of {0} is {1}.", Mymemberinfo.Name, Mymemberinfo.DeclaringType.ToString())
        Next Mymemberinfo
    End Sub
End Class


using System;
using System.IO;
using System.Reflection;

namespace MyNamespace1 
{
    interface i 
    {
        int MyVar() ;
    };
    // DeclaringType for MyVar is i.

    class A : i 
    {
        public int MyVar() { return 0; }
    };
    // DeclaringType for MyVar is A.

    class B : A 
    {
        new int MyVar() { return 0; }
    };
    // DeclaringType for MyVar is B.

    class C : A 
    {
    };
    // DeclaringType for MyVar is A.

}

namespace MyNamespace2 
{
    class Mymemberinfo 
    { 

        public static void Main(string[] args) 
        { 

            Console.WriteLine ("\nReflection.MemberInfo");

            //Get the Type and MemberInfo. 
            Type MyType =Type.GetType("System.IO.BufferedStream");
            MemberInfo[] Mymemberinfoarray = MyType.GetMembers();

            //Get and display the DeclaringType method. 
            Console.WriteLine("\nThere are {0} members in {1}.", Mymemberinfoarray.Length, MyType.FullName); 

            foreach (MemberInfo Mymemberinfo in Mymemberinfoarray) 
            {  
                Console.WriteLine("Declaring type of {0} is {1}.", Mymemberinfo.Name, Mymemberinfo.DeclaringType); 
            }
        }
    }
}

namespace MyNamespace3 
{
    class A 
    {
        virtual public void M () {}
    }
    class B: A 
    {
        override public void M () {}
    }
}


using namespace System;
using namespace System::IO;
using namespace System::Reflection;

namespace MyNamespace1
{
   interface class i
   {
      int MyVar();
   };

   // DeclaringType for MyVar is i.
   ref class A: public i
   {
   public:
      virtual int MyVar()
      {
         return 0;
      }

   };

   // DeclaringType for MyVar is A.
   ref class B: public A
   {
   private:
      int MyVar() new
      {
         return 0;
      }
   };

   // DeclaringType for MyVar is B.
   ref class C: public A{};
}

// DeclaringType for MyVar is A.
int main()
{
   Console::WriteLine( "\nReflection.MemberInfo" );

   //Get the Type and MemberInfo. 
   Type^ MyType = Type::GetType( "System.IO.BufferedStream" );
   array<MemberInfo^>^Mymemberinfoarray = MyType->GetMembers();

   //Get and display the DeclaringType method. 
   Console::WriteLine( "\nThere are {0} members in {1}.", Mymemberinfoarray->Length, MyType->FullName );
   System::Collections::IEnumerator^ enum0 = Mymemberinfoarray->GetEnumerator();
   while ( enum0->MoveNext() )
   {
      MemberInfo^ Mymemberinfo = safe_cast<MemberInfo^>(enum0->Current);
      Console::WriteLine( "Declaring type of {0} is {1}.", Mymemberinfo->Name, Mymemberinfo->DeclaringType );
   }
}

namespace MyNamespace3
{
   ref class A
   {
   public:
      virtual void M(){}

   };

   ref class B: public A
   {
   public:
      virtual void M() override {}
   };
}

NoteNote

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

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Microsoft is conducting an online survey to understand your opinion of the MSDN Web site. If you choose to participate, the online survey will be presented to you when you leave the MSDN Web site.

Would you like to participate?