MemberInfo.DeclaringType Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá třídu, která deklaruje tohoto člena.
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
Hodnota vlastnosti
Objekt Type pro třídu, která deklaruje tento člen.
Implementuje
Příklady
Následující příklad definuje rozhraní , IValues jedním členem, GetValue. Definuje také čtyři třídy: Azákladní třída, která implementuje IValue rozhraní; B, která dědí z A implementace základní třídy a skrývá její implementaci GetValue z implementace základní třídy; C, která jednoduše dědí z A; a D, která dědí z A a přepíše jeho GetValue metodu. Příklad pak načte MemberInfo objekt pro každý člen typu (včetně členů zděděných) Objecta zobrazí hodnotu jeho DeclaringType vlastnosti.
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
Všimněte si, že deklarující typ A.GetValue je , který B obsahuje dvě GetValue metody, jeden deklarován A a jeden po B, a že deklarující typ D.GetValue je DA.
Poznámka:
DeclaringType vrátí pouze názvy členů a názvy jejich deklarací typů. Chcete-li vrátit jména členů s jejich prototypy, zavolejte MemberInfo.ToString.
Poznámky
Vlastnost DeclaringType načte odkaz na Type objekt pro typ, který deklaruje tohoto člena. Člen typu je buď deklarován typem nebo zděděným ze základního typu, takže Type objekt vrácený DeclaringType vlastností nemusí být stejný jako Type objekt použitý k získání aktuálního MemberInfo objektu.
TypePokud objekt, ze kterého byl tentoMemberInfoobjekt získán, nebyl deklarována tento člen, DeclaringType bude vlastnost představovat jeden z jeho základních typů.MemberInfoPokud je objekt globálním členem (to znamená, že pokud byl získán z Module.GetMethods metody, která vrací globální metody v modulu), budenullvrácena DeclaringType .