MemberInfo.DeclaringType Property
Gets the class that declares this member.
[Visual Basic] Public MustOverride ReadOnly Property DeclaringType As Type [C#] public abstract Type DeclaringType {get;} [C++] public: __property virtual Type* get_DeclaringType() = 0; [JScript] public abstract function get DeclaringType() : Type;
Property Value
The Type object for the class that declares this member.
Remarks
The DeclaringType property retrieves a reference to the Type object for the type that declares this member. A member of a class (or interface) is either declared or inherited from a base class (or interface). The returned class might not be the same as the Type object used to obtain this 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).
Example
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.
[Visual Basic] 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 [C#] 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 () {} } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::IO; using namespace System::Reflection; namespace MyNamespace1 { __gc __interface i { int MyVar() ; }; // DeclaringType for MyVar is i. __gc class A : public i { public: int MyVar() { return 0; } }; // DeclaringType for MyVar is A. __gc class B : public A { int MyVar() { return 0; } }; // DeclaringType for MyVar is B. __gc class C : public A { }; // DeclaringType for MyVar is A. } int main() { Console::WriteLine (S"\nReflection.MemberInfo"); //Get the Type and MemberInfo. Type* MyType =Type::GetType(S"System.IO.BufferedStream"); MemberInfo* Mymemberinfoarray[] = MyType->GetMembers(); //Get and display the DeclaringType method. Console::WriteLine(S"\nThere are {0} members in {1}.", __box(Mymemberinfoarray->Length), MyType->FullName); System::Collections::IEnumerator* enum0 = Mymemberinfoarray->GetEnumerator(); while (enum0->MoveNext()) { MemberInfo* Mymemberinfo = __try_cast<MemberInfo*>(enum0->Current); Console::WriteLine(S"Declaring type of {0} is {1}.", Mymemberinfo->Name, Mymemberinfo->DeclaringType); } } namespace MyNamespace3 { __gc class A { public: virtual void M () {} }; __gc class B: public A { public: void M () {} }; } [JScript] 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 {} } }
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.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
MemberInfo Class | MemberInfo Members | System.Reflection Namespace