MemberInfo.DeclaringType 属性

定义

获取声明该成员的类。Gets the class that declares this member.

public:
 abstract property Type ^ DeclaringType { Type ^ get(); };
public abstract Type DeclaringType { get; }
public abstract Type? DeclaringType { get; }
member this.DeclaringType : Type
Public MustOverride ReadOnly Property DeclaringType As Type

属性值

Type

声明该成员的类的 Type 对象。The Type object for the class that declares this member.

实现

示例

下面的示例定义了一个 IValue 具有单个成员的接口 GetValueThe following example defines an interface, IValue, with a single member, GetValue. 它还定义了四个类:和 A 一个实现接口的基类;,它 IValue B A 从基类实现继承并隐藏其对的实现;,它从继承而来 GetValue C A D ,后者继承自 A 并重写其 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. 然后,该示例将 MemberInfo 为类型的每个成员检索一个对象 (包括继承自) 的成员 Object ,并显示其属性的值 DeclaringTypeThe 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
Imports System.Reflection

Interface IValue 
   Function GetValue() As Integer 
End Interface
 
Class A : Implements IValue 
   Public Overridable Function GetValue() As Integer _
                               Implements IValue.GetValue 
      Return 0 
   End Function
End Class
 
Class B : Inherits A 
   Public Shadows Function GetValue() As Integer 
      Return 0 
   End Function
End Class
 
Class C : Inherits A 
End Class

Class D : Inherits A
   Public Overrides Function GetValue() As Integer
      Return 0
   End Function
End Class

Public Module Example
   Public Sub Main()
      ' Get members of IValue interface.
      ShowMembersAndDeclaringTypes(GetType(IValue))
      Console.WriteLine()
      ShowMembersAndDeclaringTypes(GetType(A))
      Console.WriteLine()
      ShowMembersAndDeclaringTypes(GetType(B))
      Console.WriteLine()
      ShowMembersAndDeclaringTypes(GetType(C))
      Console.WriteLine()
      ShowMembersAndDeclaringTypes(GetType(D))
      Console.WriteLine()
   End Sub

   Private Sub ShowMembersAndDeclaringTypes(t As Type)
      Dim members() As MemberInfo = t.GetMembers()
      Console.WriteLine("{0} Members: ", t.Name)
      For Each member In members
         Console.WriteLine("   {0}, Declaring type: {1}", 
                           member.Name, member.DeclaringType.Name)
      Next                      
   End Sub
End Module
' 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

请注意,的声明类型 A.GetValueA ,其中 B 包括两个 GetValue 方法,一个由 A 和一个声明 B ,另一个是的声明类型 D.GetValue DNote 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.

备注

DeclaringType 仅返回成员名称和其声明类型的名称。DeclaringType returns only the member names and the names of their declaring types. 若要返回成员名称及其原型,请调用 MemberInfo.ToStringTo return the member names with their prototypes, call MemberInfo.ToString.

注解

DeclaringType属性检索对 Type 声明此成员的类型的对象的引用。The DeclaringType property retrieves a reference to the Type object for the type that declares this member. 类型的成员既可以由类型声明,也可以从基类型继承,因此 Type 属性返回的对象 DeclaringType 可能与 Type 用于获取当前对象的对象不同 MemberInfoA 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.

  • 如果从中 Type 获取此对象的对象 MemberInfo 未声明此成员,则该 DeclaringType 属性将表示其基类型之一。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.

  • 如果 MemberInfo 对象是全局成员 (即,如果它是从方法获取的(它 Module.GetMethods 在模块) 上返回全局方法),则返回的 DeclaringType 将为 nullIf 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.

适用于