Export (0) Print
Expand All

MemberInfo.DeclaringType Property

Gets the class that declares this member.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

public abstract Type DeclaringType { get; }
/** @property */
public abstract Type get_DeclaringType ()

public abstract function get DeclaringType () : Type

Property Value

The Type object for the class that declares this member.

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 will represent one of its base types.

  • If the MemberInfo object is a global member, (that is, it was obtained from Module.GetMethods, which returns global methods on a module), then the returned DeclaringType will be a null reference (Nothing in Visual Basic).

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.

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 () {}
    }
}

package MyNamespace2;
import System.*;
import System.IO.*;
import System.Reflection.*;

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}.", 
            String.valueOf(myMemberInfoArray.length),myType.get_FullName());
        
        for(int iCtr=0; iCtr < myMemberInfoArray.length; iCtr++) {  
            MemberInfo myMemberInfo = myMemberInfoArray[iCtr];
            Console.WriteLine("Declaring type of {0} is {1}.", 
                myMemberInfo.get_Name(),myMemberInfo.get_DeclaringType());
        }
    } //main
} //MyMemberInfo


package MyPackage1 {

 interface i {
 function MyVar() : int ;
 };
 // DeclaringType for MyVar is i.
 
 class A implements i {
 public function MyVar() : int { return 0; }
 };
 // DeclaringType for MyVar is A.
 
 class B extends A {
 hide function MyVar() :  int{ return 0; }
 };
 // DeclaringType for MyVar is B.
 
 class C extends A {
 };
 // DeclaringType for MyVar is A.
}

import System;
import System.IO;
import System.Reflection;

class Mymemberinfo { 
 
   public static function Main() : void  { 
 
    Console.WriteLine ("\nReflection.MemberInfo");
 
    //Get the Type and MemberInfo. 
    var MyType : Type =Type.GetType("System.IO.BufferedStream");
    var Mymemberinfoarray : MemberInfo[] = MyType.GetMembers();
 
    //Get and display the DeclaringType method. 
    Console.Write("\nThere are {0} members in ", Mymemberinfoarray.Length); 
    Console.Write("{0}.", MyType.FullName); 
 
    for (var i : int in Mymemberinfoarray) {
      var Mymemberinfo : MemberInfo = Mymemberinfoarray[i];  
      Console.Write("\n" + Mymemberinfo.Name + " declaring type - "
        + Mymemberinfo.DeclaringType); 
    }
   }
}
Mymemberinfo.Main();


package MyPackage3 {
class A {
    public function M () : void  {}
}
class B extends A {
    override public function M () : void  {}
}
}

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.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft