Type.GetProperties 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 Type의 속성을 가져옵니다.
오버로드
| GetProperties() |
현재 Type의 모든 public 속성을 반환합니다. |
| GetProperties(BindingFlags) |
파생 클래스에서 재정의되면, 현재 Type의 속성을 지정된 바인딩 제약 조건으로 검색합니다. |
GetProperties()
현재 Type의 모든 public 속성을 반환합니다.
public:
cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public:
virtual cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public System.Reflection.PropertyInfo[] GetProperties ();
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()
반환
현재 PropertyInfo의 모든 public 속성을 나타내는 Type 개체의 배열입니다.
또는 현재 PropertyInfo에 public 속성이 없을 경우 Type 형식의 빈 배열입니다.
구현
예제
다음 예제에서는 GetProperties 메서드를 사용하는 방법을 보여 줍니다.
array<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 ] );
}
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());
}
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
설명
이 오버로드를 호출하는 것은 C# 및 Visual Basic 과 같은 인수를 사용하여 오버로드를 호출하는 것과 GetProperties(BindingFlags) bindingAttr BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public 같습니다. 현재 개체가 나타내는 형식으로 정의된 속성과 기본 형식에서 상속된 속성인 모든 공용 인스턴스 및 정적 Type 속성을 반환합니다.
속성이 public인 접근자를 하나 이상 포함하는 경우 리플렉션에 public으로 간주됩니다. 그렇지 않으면 속성은 private으로 간주되며 | | 사용해야 BindingFlags.NonPublic BindingFlags.Instance 합니다(Visual Basic 를 사용하여 값을 BindingFlags.Static Or 결합).
GetProperties메서드는 사전순 또는 선언 순서와 같은 특정 순서로 속성을 반환하지 않습니다. 해당 순서가 달라지므로 속성이 반환되는 순서에 따라 코드가 달라지지 않아야 합니다.
다음 표에서는 형식에 반영할 때 메서드에서 반환되는 기본 클래스의 멤버를 보여 Get 있습니다.
| 멤버 형식 | 정적 | 비정적 |
|---|---|---|
| 생성자 | 아니요 | 아니요 |
| 필드 | 아니요 | 예. 필드는 항상 이름 및 서명을 숨깁니다. |
| 이벤트 | 해당 없음 | 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 hide-by-name-and-signature로 처리합니다. 아래 참고 2를 참조하세요. |
| 메서드 | 아니요 | 예. 메서드 (가상 및 비가상)는 이름을 기준으로 숨기 거 나 이름에 따라 숨길 수 있습니다. |
| 중첩 형식 | 아니요 | 아니요 |
| 속성 | 해당 없음 | 공용 형식 시스템 규칙은 상속이 속성을 구현 하는 메서드와 동일 하다는 것입니다. 리플렉션에서는 속성을 이름으로 숨기기-서명으로 처리 합니다. 아래의 참고 2를 참조 하세요. |
이름 및 시그니처는 사용자 지정 한정자, 반환 형식, 매개 변수 형식, 센티널 및 관리 되지 않는 호출 규칙을 포함 하 여 시그니처의 모든 부분을 고려 합니다. 이는 이진 비교입니다.
리플렉션의 경우 속성과 이벤트는 이름 및 시그니처로 숨겨집니다. 기본 클래스에 get 및 set 접근자가 모두 포함 된 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기고 기본 클래스의 setter에 액세스할 수 없게 됩니다.
사용자 지정 특성은 공용 형식 시스템의 일부가 아닙니다.
현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 PropertyInfo 형식 매개 변수를 적절 한 형식 인수로 대체 하 여 개체를 반환 합니다.
현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 속성을 검색 합니다.
추가 정보
적용 대상
GetProperties(BindingFlags)
파생 클래스에서 재정의되면, 현재 Type의 속성을 지정된 바인딩 제약 조건으로 검색합니다.
public:
abstract cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.PropertyInfo[] GetProperties (System.Reflection.BindingFlags bindingAttr);
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
Public MustOverride Function GetProperties (bindingAttr As BindingFlags) As PropertyInfo()
매개 변수
반환
현재 Type의 속성 중 지정된 바인딩 제약 조건과 일치하는 모든 속성을 나타내는 개체의 배열입니다.
또는 현재 PropertyInfo에 속성이 없거나 해당 바인딩 제약 조건과 일치하는 속성이 없을 경우 Type 형식의 빈 배열입니다.
구현
예제
다음 예제에서는 PropertyClass 6 개의 속성을 포함 하는 라는 클래스를 정의 합니다. 즉, 둘 중 하나는 public이 고, 하나는 protected 이며, 하나는 내부 (Visual Basic)이 고 하나는 내부에서 Friend 보호 되는 ( Protected Friend Visual Basic)입니다. 그런 다음 get , set 지정 된 바인딩 제약 조건과 일치 하는 속성에 대 한 몇 가지 기본 속성 정보 (속성 이름 및 형식, 읽기/쓰기 여부 및 해당 및 접근자의 표시 여부)를 표시 합니다.
using namespace System;
using namespace System::Reflection;
// Create a class having three properties.
public ref class PropertyClass
{
public:
property String^ Property1
{
String^ get()
{
return "hello";
}
}
property String^ Property2
{
String^ get()
{
return "hello";
}
}
protected:
property String^ Property3
{
String^ get()
{
return "hello";
}
}
private:
property int Property4
{
int get()
{
return 32;
}
}
internal:
property String^ Property5
{
String^ get()
{
return "value";
}
}
public protected:
property String^ Property6
{
String^ get()
{
return "value";
}
}
};
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";
}
void DisplayPropertyInfo(array<PropertyInfo^>^ propInfos )
{
// Display information for all properties.
for each(PropertyInfo^ 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();
}
}
void main()
{
Type^ myType = PropertyClass::typeid;
// Get the public properties.
array<PropertyInfo^>^propInfos = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
Console::WriteLine("The number of public properties: {0}.\n",
propInfos->Length);
// Display the public properties.
DisplayPropertyInfo( propInfos );
// Get the non-public properties.
array<PropertyInfo^>^propInfos1 = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
Console::WriteLine("The number of non-public properties: {0}.\n",
propInfos1->Length);
// Display all the non-public properties.
DisplayPropertyInfo(propInfos1);
}
// The example displays the following output:
// The number of public properties: 2.
//
// Property name: Property2
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// Property name: Property1
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// The number of non-public properties: 4.
//
// Property name: Property6
// Property type: System.String
// Read-Write: False
// Visibility: Protected Internal/Friend
//
// Property name: Property5
// Property type: System.String
// Read-Write: False
// Visibility: Internal/Friend
//
// Property name: Property4
// Property type: System.Int32
// Read-Write: False
// Visibility: Private
//
// Property name: Property3
// Property type: System.String
// Read-Write: False
// Visibility: Protected
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
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.NonPublic BindingFlags.Public .
다음 BindingFlags 필터 플래그는 검색에 포함할 속성을 정의 하는 데 사용할 수 있습니다.
BindingFlags.Instance인스턴스 메서드를 포함 하도록 지정 합니다.BindingFlags.Static정적 메서드를 포함 하도록 지정 합니다.BindingFlags.Public검색에 공용 속성을 포함 하도록 지정 합니다. 속성은 public 접근자가 하나 이상 있는 경우 리플렉션에 public으로 간주 됩니다.BindingFlags.NonPublic검색에 public이 아닌 속성 (즉, private, internal 및 protected 속성)을 포함 하도록 지정 합니다. 기본 클래스의 protected 및 internal 속성만 반환 됩니다. 기본 클래스의 개인 속성은 반환 되지 않습니다.BindingFlags.FlattenHierarchypublic계층의 정적 멤버를 포함 하도록 지정 합니다. 상속 된protectedprivate클래스의 정적 멤버는 포함 되지 않습니다.BindingFlags.Default빈 배열을 반환 하려면만 지정 PropertyInfo 합니다.
다음 BindingFlags 한정자 플래그를 사용 하 여 검색의 작동 방식을 변경할 수 있습니다.
BindingFlags.DeclaredOnly에 선언 된 속성만 검색 Type 하며, 단순히 상속 된 속성은 검색 하지 않습니다.
자세한 내용은 System.Reflection.BindingFlags를 참조하세요.
GetProperties메서드는 사전순 또는 선언 순서와 같은 특정 순서로 속성을 반환 하지 않습니다. 코드가 반환 되는 순서에 따라 달라 지는 것은 아닙니다.
현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 PropertyInfo 형식 매개 변수를 적절 한 형식 인수로 대체 하 여 개체를 반환 합니다.
현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 속성을 검색 합니다.
추가 정보
- PropertyInfo
- BindingFlags
- DefaultBinder
- GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])