Type.GetProperties Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Geçerli Typeöğesinin özelliklerini alır.
Aşırı Yüklemeler
| Name | Description |
|---|---|
| GetProperties() |
Geçerli Typeöğesinin tüm genel özelliklerini döndürür. |
| GetProperties(BindingFlags) |
Türetilmiş bir sınıfta geçersiz kılındığında, belirtilen bağlama kısıtlamalarını kullanarak geçerli Typeöğesinin özelliklerini arar. |
GetProperties()
- Kaynak:
- Type.cs
- Kaynak:
- Type.cs
- Kaynak:
- Type.cs
- Kaynak:
- Type.cs
- Kaynak:
- Type.cs
Geçerli Typeöğesinin tüm genel özelliklerini döndürür.
public:
cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public:
virtual cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo[] GetProperties();
public System.Reflection.PropertyInfo[] GetProperties();
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperties : unit -> System.Reflection.PropertyInfo[]
member this.GetProperties : unit -> System.Reflection.PropertyInfo[]
abstract member GetProperties : unit -> System.Reflection.PropertyInfo[]
override this.GetProperties : unit -> System.Reflection.PropertyInfo[]
Public Function GetProperties () As PropertyInfo()
Döndürülenler
Geçerli PropertyInfoöğesinin Type tüm genel özelliklerini temsil eden bir nesne dizisi.
-veya-
Geçerlinin PropertyInfo ortak özellikleri yoksa, türünde Typeboş bir dizi.
Uygulamalar
- Öznitelikler
Örnekler
Aşağıdaki örnekte yönteminin kullanımı gösterilmektedir GetProperties .
PropertyInfo[] myPropertyInfo;
// Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties();
Console.WriteLine("Properties of System.Type are:");
for (int i = 0; i < myPropertyInfo.Length; i++)
{
Console.WriteLine(myPropertyInfo[i].ToString());
}
// Get the properties of 'Type' class object.
let myPropertyInfo = Type.GetType("System.Type").GetProperties()
printfn "Properties of System.Type are:"
for pi in myPropertyInfo do
printfn $"{pi}"
Dim myPropertyInfo() As PropertyInfo
' Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties()
Console.WriteLine("Properties of System.Type are:")
Dim i As Integer
For i = 0 To myPropertyInfo.Length - 1
Console.WriteLine(myPropertyInfo(i).ToString())
Next i
Açıklamalar
Bu aşırı yüklemenin çağrılması, C# dilinde GetProperties(BindingFlags) eşit bir bindingAttr bağımsız değişkeniyle BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public aşırı yüklemesini çağırmaya ve Visual Basic'de BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public çağırmaya eşdeğerdir. Hem geçerli Type nesne tarafından temsil edilen türle tanımlanan hem de temel türlerinden devralınan tüm genel örneği ve statik özellikleri döndürür.
Bir özellik, en az bir genel erişimciye sahipse yansıma tarafından genel kabul edilir. Aksi takdirde, özellik özel olarak kabul edilir ve onu almak için BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static kullanmanız gerekir (Visual Basic'te değerleri birleştirmek için Or kullanın).
.NET 6 ve önceki sürümlerde, GetProperties yöntemi alfabetik veya bildirim sırası gibi belirli bir sırada özellik döndürmez. Kodunuz özelliklerin döndürülme sırasına bağlı olmamalıdır, çünkü bu sıra değişir. Ancak, .NET 7'den başlayarak sıralama, derlemedeki meta veri sıralamasını temel alarak belirleyicidir.
Aşağıdaki tabloda, bir türe yansıtılırken yöntemler tarafından döndürülen temel sınıf üyeleri gösterilmektedir.
| Üye Türü | Static | Statik Olmayan |
|---|---|---|
| Yapıcı | No | No |
| Alan | No | Yes. Alan her zaman isim ve imzaya göre gizli olur. |
| Olay | Uygulanamaz | Yaygın tür sistemi kuralı, özniteliği uygulayan yöntemlerdeki devralma ile aynıdır. Yansıma, özellikleri ada ve imzaya göre gizle olarak ele alır. Aşağıdaki 2. nota bakın. |
| Yöntem | No | Yes. Bir yöntem (hem sanal hem de sanal olmayan) isim odaklı gizleme veya isim ve imza odaklı gizleme olabilir. |
| İç İçe Yerleştirilmiş Tür | No | No |
| Mülkiyet | Uygulanamaz | Yaygın tür sistemi kuralı, özniteliği uygulayan yöntemlerdeki devralma ile aynıdır. Yansıma, özellikleri ada ve imzaya göre gizle olarak ele alır. Aşağıdaki 2. nota bakın. |
Ad ve imzaya göre gizleme, özel değiştiriciler, dönüş türleri, parametre türleri, sentineller ve yönetilmeyen çağırma kuralları dahil olmak üzere, imzanın tüm parçalarını dikkate alır. Bu ikili karşılaştırmadır.
Yansıma için, özellikler ve olaylar ada ve imzaya göre gizlenir. Temel sınıfta hem get hem de set erişimcisine sahip bir özelliğiniz varsa, ancak türetilen sınıfın yalnızca bir get erişimcisi varsa, türetilmiş sınıf özelliği temel sınıf özelliğini gizler ve temel sınıfta ayarlayıcıya erişemezsiniz.
Özel öznitelikler ortak tür sisteminin bir parçası değildir.
Geçerli Type , yapılandırılmış bir genel türü temsil ederse, bu yöntem nesneleri uygun tür bağımsız değişkenleriyle değiştirilen tür parametreleriyle döndürür PropertyInfo .
Geçerli Type , genel bir tür veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem sınıf kısıtlamasının özelliklerini arar.
Ayrıca bkz.
Şunlara uygulanır
GetProperties(BindingFlags)
- Kaynak:
- Type.cs
- Kaynak:
- Type.cs
- Kaynak:
- Type.cs
- Kaynak:
- Type.cs
- Kaynak:
- Type.cs
Türetilmiş bir sınıfta geçersiz kılındığında, belirtilen bağlama kısıtlamalarını kullanarak geçerli Typeöğesinin özelliklerini arar.
public:
abstract cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties(System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public abstract System.Reflection.PropertyInfo[] GetProperties(System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.PropertyInfo[] GetProperties(System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
Public MustOverride Function GetProperties (bindingAttr As BindingFlags) As PropertyInfo()
Parametreler
- bindingAttr
- BindingFlags
Aramanın nasıl yapıldığını belirten numaralandırma değerlerinin bit düzeyinde birleşimi.
-veya-
Default boş bir dizi döndürmek için.
Döndürülenler
Belirtilen bağlama kısıtlamalarıyla eşleşen geçerlinin Type tüm özelliklerini temsil eden bir nesne dizisi.
-veya-
Geçerlinin PropertyInfo özellikleri yoksa veya özelliklerin Typehiçbiri bağlama kısıtlamalarıyla eşleşmiyorsa türünde boş bir dizi.
Uygulamalar
- Öznitelikler
Örnekler
Aşağıdaki örnek, altı özellik içeren PropertyClass adlı bir sınıfı tanımlar: ikisi ortak, biri özel, biri korumalı, biri iç (Visual Basic'de Friend) ve biri de iç (Visual Basic'de Protected Friend) korumalıdır. Ardından, belirtilen bağlama kısıtlamalarıyla eşleşen özellikler için bazı temel özellik bilgilerini (özellik adı ve türü, okuma/yazma ve erişimcilerinin getset görünürlüğü) görüntüler.
using System;
using System.Reflection;
// Create a class having six properties.
public class PropertyClass
{
public String Property1
{
get { return "hello"; }
}
public String Property2
{
get { return "hello"; }
}
protected String Property3
{
get { return "hello"; }
}
private Int32 Property4
{
get { return 32; }
}
internal String Property5
{
get { return "value"; }
}
protected internal String Property6
{
get { return "value"; }
}
}
public class Example
{
public static void Main()
{
Type t = typeof(PropertyClass);
// Get the public properties.
PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public|BindingFlags.Instance);
Console.WriteLine("The number of public properties: {0}.\n",
propInfos.Length);
// Display the public properties.
DisplayPropertyInfo(propInfos);
// Get the nonpublic properties.
PropertyInfo[] propInfos1 = t.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
Console.WriteLine("The number of non-public properties: {0}.\n",
propInfos1.Length);
// Display all the nonpublic properties.
DisplayPropertyInfo(propInfos1);
}
public static void DisplayPropertyInfo(PropertyInfo[] propInfos)
{
// Display information for all properties.
foreach (var propInfo in propInfos) {
bool readable = propInfo.CanRead;
bool writable = propInfo.CanWrite;
Console.WriteLine(" Property name: {0}", propInfo.Name);
Console.WriteLine(" Property type: {0}", propInfo.PropertyType);
Console.WriteLine(" Read-Write: {0}", readable & writable);
if (readable) {
MethodInfo getAccessor = propInfo.GetMethod;
Console.WriteLine(" Visibility: {0}",
GetVisibility(getAccessor));
}
if (writable) {
MethodInfo setAccessor = propInfo.SetMethod;
Console.WriteLine(" Visibility: {0}",
GetVisibility(setAccessor));
}
Console.WriteLine();
}
}
public static String GetVisibility(MethodInfo accessor)
{
if (accessor.IsPublic)
return "Public";
else if (accessor.IsPrivate)
return "Private";
else if (accessor.IsFamily)
return "Protected";
else if (accessor.IsAssembly)
return "Internal/Friend";
else
return "Protected Internal/Friend";
}
}
// The example displays the following output:
// The number of public properties: 2.
//
// Property name: Property1
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// Property name: Property2
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// The number of non-public properties: 4.
//
// Property name: Property3
// Property type: System.String
// Read-Write: False
// Visibility: Protected
//
// Property name: Property4
// Property type: System.Int32
// Read-Write: False
// Visibility: Private
//
// Property name: Property5
// Property type: System.String
// Read-Write: False
// Visibility: Internal/Friend
//
// Property name: Property6
// Property type: System.String
// Read-Write: False
// Visibility: Protected Internal/Friend
open System.Reflection
// Create a class having four properties.
type PropertyClass() =
member _.Property1 =
"hello"
member _.Property2 =
"hello"
member private _.Property3 =
32
member internal _.Property4 =
"value"
let getVisibility (accessor: MethodInfo) =
if accessor.IsPublic then
"Public"
elif accessor.IsPrivate then
"Private"
elif accessor.IsFamily then
"Protected"
elif accessor.IsAssembly then
"Internal/Friend"
else
"Protected Internal/Friend"
let displayPropertyInfo (propInfos: #seq<PropertyInfo>) =
// Display information for all properties.
for propInfo in propInfos do
let readable = propInfo.CanRead
let writable = propInfo.CanWrite
printfn $" Property name: {propInfo.Name}"
printfn $" Property type: {propInfo.PropertyType}"
printfn $" Read-Write: {readable && writable}"
if readable then
let getAccessor = propInfo.GetMethod
printfn $" Visibility: {getVisibility getAccessor}"
if writable then
let setAccessor = propInfo.SetMethod
printfn $" Visibility: {getVisibility setAccessor}"
printfn ""
let t = typeof<PropertyClass>
// Get the public properties.
let propInfos = t.GetProperties(BindingFlags.Public ||| BindingFlags.Instance)
printfn $"The number of public properties: {propInfos.Length}.\n"
// Display the public properties.
displayPropertyInfo propInfos
// Get the nonpublic properties.
let propInfos1 = t.GetProperties(BindingFlags.NonPublic ||| BindingFlags.Instance)
printfn $"The number of non-public properties: {propInfos1.Length}.\n"
// Display all the nonpublic properties.
displayPropertyInfo propInfos1
// The example displays the following output:
// The number of public properties: 2.
//
// Property name: Property1
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// Property name: Property2
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// The number of non-public properties: 2.
//
// Property name: Property3
// Property type: System.Int32
// Read-Write: False
// Visibility: Private
//
// Property name: Property4
// Property type: System.String
// Read-Write: False
// Visibility: Internal/Friend
Imports System.Reflection
' Create a class having six properties.
Public Class PropertyClass
Public ReadOnly Property Property1() As String
Get
Return "hello"
End Get
End Property
Public ReadOnly Property Property2() As String
Get
Return "hello"
End Get
End Property
Protected ReadOnly Property Property3() As String
Get
Return "hello"
End Get
End Property
Private ReadOnly Property Property4 As Integer
Get
Return 32
End Get
End Property
Friend ReadOnly Property Property5 As String
Get
Return "value"
End Get
End Property
Protected Friend ReadOnly Property Property6 As String
Get
Return "value"
End Get
End Property
End Class
Public Module Example
Public Sub Main()
Dim t As Type = GetType(PropertyClass)
' Get the public properties.
Dim propInfos As PropertyInfo() = t.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
Console.WriteLine("The number of public properties: {0}",
propInfos.Length)
Console.WriteLine()
' Display the public properties.
DisplayPropertyInfo(propInfos)
' Get the non-public properties.
Dim propInfos1 As PropertyInfo() = t.GetProperties(BindingFlags.NonPublic Or BindingFlags.Instance)
Console.WriteLine("The number of non-public properties: {0}",
propInfos1.Length)
Console.WriteLine()
' Display all the non-public properties.
DisplayPropertyInfo(propInfos1)
End Sub
Public Sub DisplayPropertyInfo(ByVal propInfos() As PropertyInfo)
' Display information for all properties.
For Each propInfo In propInfos
Dim readable As Boolean = propInfo.CanRead
Dim writable As Boolean = propInfo.CanWrite
Console.WriteLine(" Property name: {0}", propInfo.Name)
Console.WriteLine(" Property type: {0}", propInfo.PropertyType)
Console.WriteLine(" Read-Write: {0}", readable And writable)
If readable Then
Dim getAccessor As MethodInfo = propInfo.GetMethod
Console.WriteLine(" Visibility: {0}",
GetVisibility(getAccessor))
End If
If writable Then
Dim setAccessor As MethodInfo = propInfo.SetMethod
Console.WriteLine(" Visibility: {0}",
GetVisibility(setAccessor))
End If
Console.WriteLine()
Next
End Sub
Public Function GetVisibility(accessor As MethodInfo) As String
If accessor.IsPublic Then
Return "Public"
ElseIf accessor.IsPrivate Then
Return "Private"
Else If accessor.IsFamily Then
Return "Protected"
Else If accessor.IsAssembly Then
Return "Internal/Friend"
Else
Return "Protected Internal/Friend"
End If
End Function
End Module
' The example displays the following output:
' The number of public properties: 2
'
' Property name: Property1
' Property type: System.String
' Read-Write: False
' Visibility: Public
'
' Property name: Property2
' Property type: System.String
' Read-Write: False
' Visibility: Public
'
' The number of non-public properties: 4
'
' Property name: Property3
' Property type: System.String
' Read-Write: False
' Visibility: Protected
'
' Property name: Property4
' Property type: System.Int32
' Read-Write: False
' Visibility: Private
'
' Property name: Property5
' Property type: System.String
' Read-Write: False
' Visibility: Internal/Friend
'
' Property name: Property6
' Property type: System.String
' Read-Write: False
' Visibility: Protected Internal/Friend
Açıklamalar
Aşırı yüklemenin GetProperties(BindingFlags) özellik bilgilerini başarıyla alabilmesi için, bağımsız değişkeninin bindingAttr en az bir BindingFlags.Instance ve BindingFlags.Staticile birlikte en az bir ve BindingFlags.NonPubliciçermesi BindingFlags.Public gerekir.
Aşağıdaki BindingFlags filtre bayrakları, aramaya hangi özelliklerin dahil edilebileceğini tanımlamak için kullanılabilir:
Örnek yöntemlerini eklemek için belirtin
BindingFlags.Instance.Statik yöntemleri dahil etmek için belirtin
BindingFlags.Static.Genel özellikleri aramaya dahil etmek için belirtin
BindingFlags.Public. Bir özellik, en az bir genel erişimciye sahipse yansıma tarafından genel kabul edilir.Genel olmayan özellikleri (özel, iç ve korumalı özellikler) aramaya dahil etmek için belirtin
BindingFlags.NonPublic. Yalnızca temel sınıflardaki korumalı ve iç özellikler döndürülür; temel sınıflardaki özel özellikler döndürülmüyor.Belirtin
BindingFlags.FlattenHierarchy:publicveprotectedstatik üyeleri hiyerarşiye eklemek için; türetilmiş sınıflardakiprivatestatik üyeler dahil edilmez.Boş
BindingFlags.Defaultbir dizi döndürmek için tek başına belirtinPropertyInfo.
Aramanın çalışma biçimini değiştirmek için aşağıdaki BindingFlags değiştirici bayraklar kullanılabilir:
-
BindingFlags.DeclaredOnlyyalnızca Type üzerinde bildirilen özellikleri aramak için, yalnızca devralınan özellikleri hariç tutar.
Daha fazla bilgi için bkz. System.Reflection.BindingFlags.
.NET 6 ve önceki sürümlerde, GetProperties yöntemi alfabetik veya bildirim sırası gibi belirli bir sırada özellik döndürmez. Kodunuz özelliklerin döndürülme sırasına bağlı olmamalıdır, çünkü bu sıra değişir. Ancak, .NET 7'den başlayarak sıralama, derlemedeki meta veri sıralamasını temel alarak belirleyicidir.
Geçerli Type , yapılandırılmış bir genel türü temsil ederse, bu yöntem nesneleri uygun tür bağımsız değişkenleriyle değiştirilen tür parametreleriyle döndürür PropertyInfo .
Geçerli Type , genel bir tür veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem sınıf kısıtlamasının özelliklerini arar.
Ayrıca bkz.
- PropertyInfo
- BindingFlags
- DefaultBinder
- GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])