MemberInfo.DeclaringType 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取声明该成员的类。
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
对象。
实现
示例
以下示例定义具有单个成员 GetValue
的接口 IValue
。 它还定义了四个类:A
(实现 接口的IValue
基类);B
(继承自A
基类实现并从基类实现C
中隐藏其实现GetValue
);(仅继承自 A
);和 D
(继承自 A
并重写其GetValue
方法)。 然后 MemberInfo ,该示例检索类型 (的每个成员(包括从 Object) 继承的成员)的 对象,并显示其 DeclaringType 属性的值。
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.GetValue
声明类型为 A
,其中包括 B
两 GetValue
个方法,一个由 A
声明,一个由 B
声明,并且 的 D.GetValue
声明类型为 D
。
注意
DeclaringType
仅返回成员名称及其声明类型的名称。 若要返回成员名称及其原型,请调用 MemberInfo.ToString
。
注解
属性 DeclaringType 检索对声明此成员的类型的 对象的引用 Type 。 类型的成员由类型声明或继承自基类型,因此Type
属性返回DeclaringType的对象可能与用于获取当前MemberInfo对象的对象不同Type
。
如果从中
Type
获取此MemberInfo
对象的 对象未声明此成员,则 DeclaringType 属性将表示其基类型之一。MemberInfo
如果对象是全局成员 (也就是说,如果它是从 Module.GetMethods 方法获取的,该方法返回模块) 上的全局方法,则返回DeclaringType的 将为null
。