Nyelv

Type.GetProperties Metódus

Definíció

Lekéri az aktuális Typetulajdonságot.

Túlterhelések

Név Description
GetProperties()

Az aktuális Typenyilvános tulajdonságokat adja eredményül.

GetProperties(BindingFlags)

Ha egy származtatott osztályban felül van bírálva, a megadott kötési megkötések használatával megkeresi az aktuális Typetulajdonságokat.

GetProperties()

Forrás:
Type.cs
Forrás:
Type.cs
Forrás:
Type.cs
Forrás:
Type.cs
Forrás:
Type.cs

Az aktuális Typenyilvános tulajdonságokat adja eredményül.

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()

Válaszok

Az aktuális PropertyInfonyilvános tulajdonságokat képviselő objektumtömbType.

-vagy-

Üres típusú PropertyInfotömb, ha az aktuális Type nem rendelkezik nyilvános tulajdonságokkal.

Megvalósítás

Attribútumok

Példák

Az alábbi példa a GetProperties metódus használatát mutatja be.

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

Megjegyzések

A túlterhelés meghívása egyenértékű a GetProperties(BindingFlags) túlterhelés meghívásával egy bindingAttr argumentummal, amely a C# BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public és a Visual Basic BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public argumentummal egyenlő. Visszaadja az összes nyilvános példányt és statikus tulajdonságot, mind az aktuális Type objektum által képviselt típus, mind az alaptípustól örökölt tulajdonságokat.

A tulajdonság akkor tekinthető nyilvánosnak a reflexió szemszögéből, ha legalább egy nyilvános hozzáférővel rendelkezik. Ellenkező esetben a tulajdonság privátnak minősül, és a lekéréshez (a Visual Basicben az értékeket kombinálva) kell használniaBindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static.Or

A .NET 6-os és korábbi verzióiban a GetProperties metódus nem ad vissza tulajdonságokat egy adott sorrendben, például betűrendben vagy deklarációs sorrendben. A kód nem függ a tulajdonságok visszaadási sorrendjétől, mert ez a sorrend eltérő. A 7. .NET kezdve azonban a rendezés determinisztikus a szerelvény metaadatainak sorrendje alapján.

Az alábbi táblázat azt mutatja be, hogy egy alaposztály mely tagjait adják vissza a metódusok egy Get típuson való tükrözéskor.

Tagtípus Static Nem statikus
Constructor No No
Field No Yes. A mezők mindig név és aláírás alapján rejthetők el.
Esemény Nem alkalmazható A gyakori típusrendszerszabály az, hogy az öröklés megegyezik a tulajdonságot megvalósító metódusok öröklésével. A tükröződés a tulajdonságokat név és aláírás elrejtéseként kezeli. Lásd az alábbi 2. megjegyzést.
Módszer No Yes. A metódusok (mind a virtuális, mind a nem virtuális) elrejthetők név szerint vagy elrejthetők név és aláírás alapján.
Beágyazott típus No No
Ingatlan Nem alkalmazható A gyakori típusrendszerszabály az, hogy az öröklés megegyezik a tulajdonságot megvalósító metódusok öröklésével. A tükröződés a tulajdonságokat név és aláírás elrejtéseként kezeli. Lásd az alábbi 2. megjegyzést.
  1. A név és aláírás szerinti elrejtés az aláírás összes részét figyelembe veszi, beleértve az egyéni módosítókat, a visszatérési típusokat, a paramétertípusokat, a sentineleket és a nem kezelt hívási konvenciókat. Ez egy bináris összehasonlítás.

  2. A reflexió során a tulajdonságok és események név és szignatúra szerint vannak elrejtve. Ha az alaposztálynak van egy beolvasási és egy beállítási kiegészítővel rendelkező tulajdonsága, de a származtatott osztálynak csak beolvasási kiegészítője van, a származtatott osztály tulajdonsága elrejti az alaposztály tulajdonságát, és nem tudja majd elérni a beállítót az alaposztályban.

  3. Az egyéni attribútumok nem részei a gyakori típusrendszernek.

Ha az áram Type egy konstruktált általános típust jelöl, ez a metódus az PropertyInfo objektumokat a megfelelő típusargumentumokkal helyettesített típusparaméterekkel adja vissza.

Ha az aktuális Type érték egy típusparamétert jelöl egy általános típus vagy általános metódus definíciójában, ez a metódus megkeresi az osztálykényszer tulajdonságait.

Lásd még

A következőre érvényes:

GetProperties(BindingFlags)

Forrás:
Type.cs
Forrás:
Type.cs
Forrás:
Type.cs
Forrás:
Type.cs
Forrás:
Type.cs

Ha egy származtatott osztályban felül van bírálva, a megadott kötési megkötések használatával megkeresi az aktuális Typetulajdonságokat.

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()

Paraméterek

bindingAttr
BindingFlags

A számbavételi értékek bitenkénti kombinációja, amely meghatározza a keresés menetét.

-vagy-

Default üres tömb visszaadásához.

Válaszok

Az aktuális Type összes tulajdonságát képviselő objektumtömb, amely megfelel a megadott kötési megkötéseknek.

-vagy-

Üres típusú PropertyInfotömb, ha az aktuális Type nem rendelkezik tulajdonságokkal, vagy ha egyik tulajdonság sem felel meg a kötési korlátozásoknak.

Megvalósítás

Attribútumok

Példák

Az alábbi példa egy PropertyClass nevű osztályt határoz meg, amely hat tulajdonságot tartalmaz: kettő nyilvános, az egyik privát, a másik belső (Friend a Visual Basic) és egy belső (Protected Friend Visual Basic). Ezután megjeleníti a megadott kötési korlátozásoknak megfelelő tulajdonságok alapvető tulajdonságadatait (a tulajdonság nevét és típusát, olvasási/írási állapotát, valamint annak és get tartozékainak láthatóságátset).

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

Megjegyzések

Ahhoz, hogy a GetProperties(BindingFlags) túlterhelés sikeresen lekérje a tulajdonságinformációkat, az bindingAttr argumentumnak tartalmaznia BindingFlags.InstanceBindingFlags.Static kell legalább az egyiket és az egyiket is.BindingFlags.NonPublicBindingFlags.Public

A következő BindingFlags szűrőjelölőkkel határozhatja meg, hogy mely tulajdonságok szerepeljenek a keresésben:

  • Adja meg BindingFlags.Instance , hogy a példány metódusai is szerepeljenek.

  • Adja meg BindingFlags.Static , hogy statikus metódusokat is tartalmazzon.

  • Adja meg BindingFlags.Public , hogy a keresésbe belefoglalja a nyilvános tulajdonságokat. A tulajdonság akkor tekinthető nyilvánosnak a reflexió szemszögéből, ha legalább egy nyilvános hozzáférővel rendelkezik.

  • Adja meg BindingFlags.NonPublic , hogy a keresésbe be legyenek foglalva a nem nyilvános tulajdonságok (azaz a privát, a belső és a védett tulajdonságok). A rendszer csak az alaposztályok védett és belső tulajdonságait adja vissza; a rendszer nem adja vissza az alaposztályok privát tulajdonságait.

  • Adja meg a BindingFlags.FlattenHierarchy-t, hogy a hierarchiába belefoglalja a public és protected statikus tagokat; az örökölt osztályokban lévő private statikus tagok nem szerepelnek.

  • Adjon meg BindingFlags.Default önállóan egy üres PropertyInfo tömb visszaadásához.

A keresés működésének módosításához az alábbi BindingFlags módosító jelzők használhatók:

  • BindingFlags.DeclaredOnly úgy keresni, hogy csak a rajta deklarált tulajdonságokra összpontosítsunk Type, nem pedig az egyszerűen öröklött tulajdonságokra.

További információért lásd a System.Reflection.BindingFlags jelű részt.

A .NET 6-os és korábbi verzióiban a GetProperties metódus nem ad vissza tulajdonságokat egy adott sorrendben, például betűrendben vagy deklarációs sorrendben. A kód nem függ a tulajdonságok visszaadási sorrendjétől, mert ez a sorrend eltérő. A 7. .NET kezdve azonban a rendezés determinisztikus a szerelvény metaadatainak sorrendje alapján.

Ha az áram Type egy konstruktált általános típust jelöl, ez a metódus az PropertyInfo objektumokat a megfelelő típusargumentumokkal helyettesített típusparaméterekkel adja vissza.

Ha az aktuális Type érték egy típusparamétert jelöl egy általános típus vagy általános metódus definíciójában, ez a metódus megkeresi az osztálykényszer tulajdonságait.

Lásd még

A következőre érvényes: