Compartir a través de


MemberInfo.DeclaringType Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the class that declares this member.

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

Syntax

'Declaration
Public MustOverride ReadOnly Property DeclaringType As Type
public abstract Type DeclaringType { get; }

Property Value

Type: System.Type
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 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 nulla null reference (Nothing in Visual Basic).

Examples

The following example shows how DeclaringType works with classes and interfaces. It uses a helper method named FormatDeclaringType to show how the declaring type of method M changes in classes that implement interfaces or that override and inherit from base classes.

For example, when class B overrides virtual method M from class A, it essentially redefines (or redeclares) this method. Therefore, the MethodInfo for B.M reports the declaring type as B instead of A, although A is where this method was originally declared.

The example also lists the names and declaring types of all the members of class C.

Imports System.Reflection

Interface I
   Function M() As String
End Interface
' DeclaringType for M is I.

Class A
   Implements I
   Public Overridable Function M() As String Implements I.M
      Return "Implemented in A."
   End Function
End Class
' DeclaringType for M is A.

Class B
   Inherits A
   Public Overrides Function M() As String
      Return "Overridden in B."
   End Function
End Class
' DeclaringType for M is B.

Class C
   Inherits A
End Class
' DeclaringType for M is A.


Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text += FormatDeclaringType(GetType(I), "M")
      outputBlock.Text += FormatDeclaringType(GetType(A), "M")
      outputBlock.Text += FormatDeclaringType(GetType(B), "M")
      outputBlock.Text += FormatDeclaringType(GetType(C), "M")

      ' Display the DeclaringType of all public members of C.
      Dim t As Type = GetType(C)
      Dim members() As MemberInfo = t.GetMembers()

      outputBlock.Text += String.Format(vbLf & "There are {0} members in {1}:" & vbLf, _
         members.Length, t.Name)

      For Each m As MemberInfo In members
         outputBlock.Text += _
            String.Format("The declaring type of {0} is {1}." & vbLf, _
               m.Name, m.DeclaringType.FullName)
      Next m
   End Sub

   Private Shared Function FormatDeclaringType(ByVal t As Type, ByVal m As String) As String
      Dim member As MemberInfo = t.GetMethod(m)
      return String.Format("The declaring type of {0}, in type {1}, is {2}." & vbLf, _
         member.Name, t.Name, member.DeclaringType.Name)
   End Function
End Class

' This example produces the following output:
'
'The declaring type of M, in type I, is I.
'The declaring type of M, in type A, is A.
'The declaring type of M, in type B, is B.
'The declaring type of M, in type C, is A.
'
'There are 6 members in C:
'The declaring type of M is SilverlightApplication.A.
'The declaring type of ToString is System.Object.
'The declaring type of Equals is System.Object.
'The declaring type of GetHashCode is System.Object.
'The declaring type of GetType is System.Object.
'The declaring type of .ctor is SilverlightApplication.C.
using System;
using System.Reflection;

interface I
{
   string M();
}
// DeclaringType for M is I.

class A: I
{
   public virtual string M()
   {
      return "Implemented in A.";
   }
}
// DeclaringType for M is A.

class B: A
{
   public override string M()
   {
      return "Overridden in B.";
   }
}
// DeclaringType for M is B.

class C: A {}
// DeclaringType for M is A.


class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += FormatDeclaringType(typeof(I), "M");
      outputBlock.Text += FormatDeclaringType(typeof(A), "M");
      outputBlock.Text += FormatDeclaringType(typeof(B), "M");
      outputBlock.Text += FormatDeclaringType(typeof(C), "M");

      // Display the DeclaringType of all public members of C.
      Type t = typeof(C);
      MemberInfo[] members = t.GetMembers();

      outputBlock.Text += String.Format("\nThere are {0} members in {1}:\n",
         members.Length, t.Name);

      foreach(MemberInfo m in members)
      {
         outputBlock.Text += String.Format("The declaring type of {0} is {1}.\n",
            m.Name, m.DeclaringType.FullName);
      }
   }

   private static string FormatDeclaringType(Type t, string m)
   {
      MemberInfo member = t.GetMethod(m);
      return String.Format("The declaring type of {0}, in type {1}, is {2}.\n",
         member.Name, t.Name, member.DeclaringType.Name);
   }
}

/* This example produces the following output:

The declaring type of M, in type I, is I.
The declaring type of M, in type A, is A.
The declaring type of M, in type B, is B.
The declaring type of M, in type C, is A.

There are 6 members in C:
The declaring type of M is A.
The declaring type of ToString is System.Object.
The declaring type of Equals is System.Object.
The declaring type of GetHashCode is System.Object.
The declaring type of GetType is System.Object.
The declaring type of .ctor is C.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.