Type.GetProperties 메서드

정의

현재 Type.의 속성을 가져옵니다.

오버로드

Name Description
GetProperties()

현재 Type모든 public 속성을 반환합니다.

GetProperties(BindingFlags)

파생 클래스에서 재정의되는 경우 지정된 바인딩 제약 조건을 사용하여 현재 Type속성을 검색합니다.

GetProperties()

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

현재 Type모든 public 속성을 반환합니다.

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

반품

현재PropertyInfoType 모든 public 속성을 나타내는 개체의 배열입니다.

-또는-

현재 PropertyInfo 에 public 속성이 없는 경우 형식Type의 빈 배열입니다.

구현

특성

예제

다음 예제에서는 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

설명

이 오버로드를 호출하는 것은 GetProperties(BindingFlags) 오버로드를 bindingAttr 인수가 C#의 BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public 및 Visual Basic BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public 호출하는 것과 같습니다. 현재 Type 개체가 나타내는 형식과 기본 형식에서 상속된 형식으로 정의된 모든 공용 인스턴스 및 정적 속성을 반환합니다.

속성은 공용 접근자가 하나 이상 있으면 리플렉션에서 공용으로 간주됩니다. 그렇지 않으면 속성이 프라이빗으로 간주되며, 속성을 얻으려면 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static를 사용해야 합니다 (Visual Basic에서는 Or를 사용하여 값을 결합합니다).

.NET 6 이전 버전에서 GetProperties 메서드는 사전순 또는 선언 순서와 같은 특정 순서로 속성을 반환하지 않습니다. 해당 순서가 다르므로 코드는 속성이 반환되는 순서에 따라 달라지지 않아야 합니다. 그러나 .NET 7부터는 어셈블리의 메타데이터 순서에 따라 순서가 결정적입니다.

다음 표에서는 형식을 반영할 때 메서드에서 반환되는 기본 클래스의 Get 멤버를 보여 줍니다.

멤버 형식 Static 비정적
생성자 No No
Field No Yes. 필드는 항상 이름과 서명을 기준으로 숨겨집니다.
이벤트 적용할 수 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요.
Method No Yes. 메서드(가상 및 가상이 아닌 메서드)는 이름으로 숨기기 또는 이름과 서명으로 숨기기가 가능합니다.
중첩 타입 No No
재산 적용할 수 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요.
  1. 이름별 및 서명에 의한 숨기기는 사용자 지정 한정자, 반환 형식, 매개 변수 형식, 센티넬 및 비관리 호출 규칙을 포함하여 서명의 모든 부분을 고려합니다. 이진 비교입니다.

  2. 리플렉션의 경우 속성과 이벤트는 이름 및 서명별로 숨겨집니다. 기본 클래스에 get 및 set 접근자가 모두 있는 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기며 기본 클래스의 setter에 액세스할 수 없습니다.

  3. 사용자 지정 특성은 공용 형식 시스템의 일부가 아닙니다.

현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 형식 매개 변수가 적절한 형식 인수로 대체된 개체를 반환 PropertyInfo 합니다.

현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 속성을 검색합니다.

추가 정보

적용 대상

GetProperties(BindingFlags)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

파생 클래스에서 재정의되는 경우 지정된 바인딩 제약 조건을 사용하여 현재 Type속성을 검색합니다.

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

매개 변수

bindingAttr
BindingFlags

검색을 수행하는 방법을 지정하는 열거형 값의 비트 조합입니다.

-또는-

Default 빈 배열을 반환합니다.

반품

지정된 바인딩 제약 조건과 일치하는 현재 Type 속성의 모든 속성을 나타내는 개체의 배열입니다.

-또는-

현재 PropertyInfo 에 속성이 없거나 바인딩 제약 조건과 일치하는 속성이 없는 경우 형식Type의 빈 배열입니다.

구현

특성

예제

다음 예제에서는 6개의 속성을 포함하는 PropertyClass라는 클래스를 정의합니다. 2개는 public이고, 하나는 프라이빗이고, 하나는 보호되고, 다른 하나는 내부(Visual Basic Friend)이고, 하나는 내부로 보호됩니다(Protected Friend Visual Basic). 그런 다음 지정된 바인딩 제약 조건과 일치하는 속성에 대한 몇 가지 기본 속성 정보(속성 이름 및 형식, 읽기/쓰기 여부 및 해당 get 속성 및 set 접근자의 표시 여부)를 표시합니다.

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

설명

오버로드가 GetProperties(BindingFlags) 속성 정보를 bindingAttr 성공적으로 검색하려면 인수에 하나 이상의 BindingFlags.Instance 인수와 BindingFlags.Static하나 이상의 BindingFlags.NonPublicBindingFlags.Public인수가 포함되어야 합니다.

다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 속성을 정의할 수 있습니다.

  • 인스턴스 메서드를 포함하도록 지정 BindingFlags.Instance 합니다.

  • 정적 메서드를 포함하도록 지정 BindingFlags.Static 합니다.

  • 검색에 공용 속성을 포함하도록 지정 BindingFlags.Public 합니다. 속성은 공용 접근자가 하나 이상 있으면 리플렉션에서 공용으로 간주됩니다.

  • 검색에 public이 아닌 속성(즉, 프라이빗, 내부 및 보호된 속성)을 포함하도록 지정 BindingFlags.NonPublic 합니다. 기본 클래스의 보호된 속성과 내부 속성만 반환됩니다. 기본 클래스의 private 속성은 반환되지 않습니다.

  • 위계에서 BindingFlags.FlattenHierarchypublic 정적 멤버를 포함하도록 protected을(를) 지정하세요. 상속된 클래스에서는 private 정적 멤버가 포함되지 않습니다.

  • BindingFlags.Default 배열을 반환하려면 단독으로 지정 PropertyInfo 합니다.

다음 BindingFlags 한정자 플래그를 사용하여 검색 작동 방식을 변경할 수 있습니다.

  • BindingFlags.DeclaredOnly 단순히 상속된 속성을 검색하지 않고 Type에 선언된 속성만 검색합니다.

자세한 내용은 System.Reflection.BindingFlags을 참조하세요.

.NET 6 이전 버전에서 GetProperties 메서드는 사전순 또는 선언 순서와 같은 특정 순서로 속성을 반환하지 않습니다. 해당 순서가 다르므로 코드는 속성이 반환되는 순서에 따라 달라지지 않아야 합니다. 그러나 .NET 7부터는 어셈블리의 메타데이터 순서에 따라 순서가 결정적입니다.

현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 형식 매개 변수가 적절한 형식 인수로 대체된 개체를 반환 PropertyInfo 합니다.

현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 속성을 검색합니다.

추가 정보

적용 대상