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
,並從基類實作中隱藏其 GetValue
實作;直接繼承自 A
; C
以及 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
。