Type.GetProperty 메서드

정의

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

오버로드

GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 속성을 지정된 바인딩 제약 조건으로 검색합니다.

GetProperty(String, Type, Type[], ParameterModifier[])

지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 public 속성을 검색합니다.

GetProperty(String, Type[])

지정된 인수 형식과 일치하는 매개 변수를 가진 지정된 public 속성을 검색합니다.

GetProperty(String, Type, Type[])

지정된 인수 형식과 일치하는 매개 변수를 가진 지정된 public 속성을 검색합니다.

GetProperty(String, BindingFlags)

지정된 속성을 지정된 바인딩 제약 조건으로 검색합니다.

GetProperty(String)

지정된 이름의 public 속성을 검색합니다.

GetProperty(String, Type)

지정된 이름과 반환 형식의 public 속성을 검색합니다.

GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

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

지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 속성을 지정된 바인딩 제약 조건으로 검색합니다.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.PropertyInfo? GetProperty (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.PropertyInfo GetProperty (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
abstract member GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags, binder As Binder, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo

매개 변수

name
String

가져올 속성의 이름이 포함된 문자열입니다.

bindingAttr
BindingFlags

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

또는

null을 반환하는 Default입니다.

binder
Binder

속성 집합을 정의하고 바인딩을 활성화하는 개체입니다. 이 개체는 또한 리플렉션을 통해 오버로드된 메서드를 선택할 수 있고 인수 형식을 강제 변환할 수 있으며 멤버를 호출할 수도 있습니다.

또는

Nothing를 사용하려면 null 참조(Visual Basic의 경우 DefaultBinder)입니다.

returnType
Type

속성의 반환 형식입니다.

types
Type[]

가져올 인덱싱된 속성에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.

또는

인덱싱되지 않은 속성을 가져오기 위한 Type 형식의 빈 배열입니다(즉, Type[] types = new Type[0]).

modifiers
ParameterModifier[]

ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types 개체 배열입니다. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.

반환

지정된 요구 사항과 일치하는 속성을 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

예외

지정한 이름을 가지며 지정된 바인딩 제약 조건과 일치하는 속성이 둘 이상 발견되었습니다.

name이(가) null인 경우

또는

types이(가) null인 경우

types가 다차원 배열인 경우

또는

modifiers가 다차원 배열인 경우

또는

typesmodifiers의 길이가 동일하지 않습니다.

types의 요소가 null입니다.

설명

이 API에 대한 자세한 내용은 Type.GetProperty에 대한 추가 API 설명을 참조하세요.

추가 정보

적용 대상

GetProperty(String, Type, Type[], ParameterModifier[])

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

지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 public 속성을 검색합니다.

public:
 System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo

매개 변수

name
String

가져올 공용 속성의 이름이 포함된 문자열입니다.

returnType
Type

속성의 반환 형식입니다.

types
Type[]

가져올 인덱싱된 속성에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.

또는

인덱싱되지 않은 속성을 가져오기 위한 Type 형식의 빈 배열입니다(즉, Type[] types = new Type[0]).

modifiers
ParameterModifier[]

ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types 개체 배열입니다. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.

반환

지정된 요구 사항과 일치하는 public 속성을 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

예외

지정한 이름을 가지며 지정된 인수 형식 및 수정자와 일치하는 속성이 둘 이상 발견되었습니다.

name이(가) null인 경우

또는

types이(가) null인 경우

types가 다차원 배열인 경우

또는

modifiers가 다차원 배열인 경우

또는

typesmodifiers의 길이가 동일하지 않습니다.

types의 요소가 null입니다.

예제

다음 예제에서는 에 해당하는 개체를 Type 가져오고 이 클래스의 인덱싱된 속성은 메서드에 전달된 인수를 사용하여 검색됩니다GetProperty.MyPropertyClass

using namespace System;
using namespace System::Reflection;
public ref class MyPropertyClass
{
private:
   array<int, 2>^ myPropertyArray;

public:

   property int Item [int, int]
   {
      // Declare an indexer.
      int get( int i, int j )
      {
         return myPropertyArray[ i,j ];
      }

      void set( int i, int j, int value )
      {
         myPropertyArray[ i,j ] = value;
      }

   }

};

int main()
{
   try
   {
      Type^ myType = MyPropertyClass::typeid;
      array<Type^>^myTypeArray = gcnew array<Type^>(2);
      
      // Create an instance of the Type array representing the number, order
      // and type of the parameters for the property.
      myTypeArray->SetValue( int::typeid, 0 );
      myTypeArray->SetValue( int::typeid, 1 );
      
      // Search for the indexed property whose parameters match the
      // specified argument types and modifiers.
      PropertyInfo^ myPropertyInfo = myType->GetProperty( "Item", int::typeid, myTypeArray, nullptr );
      Console::WriteLine( "{0}.{1} has a property type of {2}", myType->FullName, myPropertyInfo->Name, myPropertyInfo->PropertyType );
   }
   catch ( Exception^ ex ) 
   {
      Console::WriteLine( "An exception occurred {0}", ex->Message );
   }

}
using System;
using System.Reflection;

public class MyPropertyClass
{
    private readonly int [,] _myPropertyArray = new int[10,10];
    // Declare an indexer.
    public int this [int i,int j]
    {
        get
        {
            return _myPropertyArray[i,j];
        }
        set
        {
            _myPropertyArray[i,j] = value;
        }
    }
}

public class MyTypeClass
{
    public static void Main()
    {
        try
        {
            Type myType=typeof(MyPropertyClass);
            Type[] myTypeArray = new Type[2];

            // Create an instance of the Type array representing the number, order
            // and type of the parameters for the property.
            myTypeArray.SetValue(typeof(int),0);
            myTypeArray.SetValue(typeof(int),1);

            // Search for the indexed property whose parameters match the
            // specified argument types and modifiers.
            PropertyInfo myPropertyInfo = myType.GetProperty("Item",
                typeof(int),myTypeArray,null);
            Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name +
                " has a property type of " + myPropertyInfo.PropertyType);
         }
        catch(Exception ex)
        {
            Console.WriteLine("An exception occurred " + ex.Message);
        }
    }
}
open System

type MyPropertyClass() =
    let myPropertyArray = Array2D.zeroCreate<int> 10 10
    // Declare an indexed property.
    member _.Item
        with get (i, j) =
            myPropertyArray[i, j]
        and set (i, j) value =
            myPropertyArray[i, j] <- value

try
    let myType = typeof<MyPropertyClass>
    let myTypeArray = Array.zeroCreate<Type> 2
    // Create an instance of the Type array representing the number, order
    // and type of the parameters for the property.
    myTypeArray.SetValue(typeof<int>, 0)
    myTypeArray.SetValue(typeof<int>, 1)
    // Search for the indexed property whose parameters match the
    // specified argument types and modifiers.
    let myPropertyInfo = myType.GetProperty("Item", typeof<int>, myTypeArray, null)
    printfn $"{myType.FullName}.{myPropertyInfo.Name} has a property type of {myPropertyInfo.PropertyType}"
with ex ->
    printfn $"An exception occurred {ex.Message}"
Imports System.Reflection

Public Class MyPropertyClass
    Private myPropertyArray(9, 9) As Integer
    ' Declare an indexer.
    Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
        Get
            Return myPropertyArray(i, j)
        End Get
        Set(ByVal Value As Integer)
            myPropertyArray(i, j) = Value
        End Set
    End Property
End Class

Public Class MyTypeClass
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyPropertyClass)
            Dim myTypeArray(1) As Type
            ' Create an instance of a Type array representing the number, order 
            ' and type of the parameters for the property.
            myTypeArray.SetValue(GetType(Integer), 0)
            myTypeArray.SetValue(GetType(Integer), 1)
            ' Search for the indexed property whose parameters match the
            ' specified argument types and modifiers.
            Dim myPropertyInfo As PropertyInfo = myType.GetProperty("Item", _
                  GetType(Integer), myTypeArray, Nothing)
            Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name + _
                  " has a property  type of " + myPropertyInfo.PropertyType.ToString())
        Catch ex As Exception
            Console.WriteLine("An exception occurred " + ex.Message.ToString())
        End Try
    End Sub
End Class

설명

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

기본 바인더는 (modifiers매개 변수)를 처리하지 않지만 추상 System.Reflection.Binder 클래스를 사용하여 를 처리하는 ParameterModifiermodifiers사용자 지정 바인더를 작성할 수 있습니다. ParameterModifier 는 COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다.

에 대한 name 검색은 대/소문자를 구분합니다. 검색에는 공용 정적 및 공용 instance 속성이 포함됩니다.

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

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

추가 정보

적용 대상

GetProperty(String, Type[])

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

지정된 인수 형식과 일치하는 매개 변수를 가진 지정된 public 속성을 검색합니다.

public:
 System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
public System.Reflection.PropertyInfo? GetProperty (string name, Type[] types);
public System.Reflection.PropertyInfo GetProperty (string name, Type[] types);
member this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, types As Type()) As PropertyInfo

매개 변수

name
String

가져올 공용 속성의 이름이 포함된 문자열입니다.

types
Type[]

가져올 인덱싱된 속성에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.

또는

인덱싱되지 않은 속성을 가져오기 위한 Type 형식의 빈 배열입니다(즉, Type[] types = new Type[0]).

반환

지정된 인수 형식과 일치하는 매개 변수를 가진 공용 속성을 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

예외

지정한 이름을 가지며 지정된 인수 형식과 일치하는 속성이 둘 이상 발견되었습니다.

name이(가) null인 경우

또는

types이(가) null인 경우

types가 다차원 배열인 경우

types의 요소가 null입니다.

예제

다음 예제에서는 사용자 정의 클래스의 개체를 검색 Type 하고, 해당 클래스의 속성을 검색하고, 에 전달된 인수에 지정된 속성의 속성 이름과 형식을 GetProperty표시합니다.

using namespace System;
using namespace System::Reflection;
ref class MyClass1
{
private:
   array<int, 2>^myArray;

public:

   property int Item [int, int]
   {

      // Declare an indexer.
      int get( int i, int j )
      {
         return myArray[ i,j ];
      }

      void set( int i, int j, int value )
      {
         myArray[ i,j ] = value;
      }

   }

};

int main()
{
   try
   {
      
      // Get the Type object.
      Type^ myType = MyClass1::typeid;
      array<Type^>^myTypeArr = gcnew array<Type^>(2);
      
      // Create an instance of a Type array.
      myTypeArr->SetValue( int::typeid, 0 );
      myTypeArr->SetValue( int::typeid, 1 );
      
      // Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
      PropertyInfo^ myPropInfo = myType->GetProperty( "Item", myTypeArr );
      
      // Display the property.
      Console::WriteLine( "The {0} property exists in MyClass1.", myPropInfo );
   }
   catch ( NullReferenceException^ e ) 
   {
      Console::WriteLine( "An exception occurred." );
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }

}

using System;
using System.Reflection;

class MyClass3
{
    private readonly int[,] _myArray = { { 1, 2 }, { 3, 4 } };
    // Declare an indexer.
    public int this[int i, int j]
    {
        get
        {
            return _myArray[i, j];
        }
        set
        {
            _myArray[i, j] = value;
        }
    }
}

public class MyTypeClass3
{
    public static void Main(string[] args)
    {
        try
        {
            // Get the Type object.
            Type myType = typeof(MyClass3);
            Type[] myTypeArr = new Type[2];

            // Create an instance of a Type array.
            myTypeArr.SetValue(typeof(int), 0);
            myTypeArr.SetValue(typeof(int), 1);

            // Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
            PropertyInfo myPropInfo = myType.GetProperty("Item", myTypeArr);

            // Display the property.
            Console.WriteLine("The {0} property exists in MyClass3.",
                myPropInfo.ToString());
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("An exception occurred.");
            Console.WriteLine("Source : {0}", e.Source);
            Console.WriteLine("Message : {0}", e.Message);
        }
    }
}
open System

type MyClass1() =
    let myArray = array2D [[1; 2]; [3; 4]]
    // Declare an indexed property.
    member _.Item
        with get (i, j) =
            myArray[i, j]
        and set (i, j) value =
            myArray[i, j] <- value
try
    // Get the Type object.
    let myType = typeof<MyClass1>
    let myTypeArr = Array.zeroCreate<Type> 2
    // Create an instance of a Type array.
    myTypeArr.SetValue(typeof<int>, 0)
    myTypeArr.SetValue(typeof<int>, 1)
    // Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
    let myPropInfo = myType.GetProperty("Item", myTypeArr)
    // Display the property.
    printfn $"The {myPropInfo} property exists in MyClass1."
with :? NullReferenceException as e ->
    printfn "An exception occurred."
    printfn $"Source : {e.Source}" 
    printfn $"Message : {e.Message}"
Imports System.Reflection

Module Module1
    Class MyClass1
        Private myArray As Integer(,) = {{1, 2}, {3, 4}}
        ' Declare an indexer.
        Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
            Get
                Return myArray(i, j)
            End Get
            Set(ByVal Value As Integer)

                myArray(i, j) = Value
            End Set
        End Property
    End Class

    Public Class MyTypeClass
        Public Shared Sub Main()
            Try
                ' Get the Type Object.
                Dim myType As Type = GetType(MyClass1)
                Dim myTypeArr(1) As Type
                ' Create an instance of a Type array.
                myTypeArr.SetValue(GetType(Integer), 0)
                myTypeArr.SetValue(GetType(Integer), 1)
                ' Get the PropertyInfo object for the indexed property Item, which has two integer parameters. 
                Dim myPropInfo As PropertyInfo = myType.GetProperty("Item", myTypeArr)
                ' Display the property.
                Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.ToString())
            Catch e As NullReferenceException
                Console.WriteLine("An exception occurred.")
                Console.WriteLine("Source : {0}", e.Source.ToString())
                Console.WriteLine("Message : {0}", e.Message.ToString())
            End Try
        End Sub
    End Class
End Module 'Module1

설명

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

에 대한 name 검색은 대/소문자를 구분합니다. 검색에는 공용 정적 및 공용 instance 속성이 포함됩니다.

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

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

추가 정보

적용 대상

GetProperty(String, Type, Type[])

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

지정된 인수 형식과 일치하는 매개 변수를 가진 지정된 public 속성을 검색합니다.

public:
 System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType, Type[] types);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types);
member this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type()) As PropertyInfo

매개 변수

name
String

가져올 공용 속성의 이름이 포함된 문자열입니다.

returnType
Type

속성의 반환 형식입니다.

types
Type[]

가져올 인덱싱된 속성에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.

또는

인덱싱되지 않은 속성을 가져오기 위한 Type 형식의 빈 배열입니다(즉, Type[] types = new Type[0]).

반환

지정된 인수 형식과 일치하는 매개 변수를 가진 공용 속성을 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

예외

지정한 이름을 가지며 지정된 인수 형식과 일치하는 속성이 둘 이상 발견되었습니다.

name이(가) null인 경우

또는

types이(가) null인 경우

types가 다차원 배열인 경우

types의 요소가 null입니다.

설명

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

에 대한 name 검색은 대/소문자를 구분합니다. 검색에는 공용 정적 및 공용 instance 속성이 포함됩니다.

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

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

추가 정보

적용 대상

GetProperty(String, BindingFlags)

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

지정된 속성을 지정된 바인딩 제약 조건으로 검색합니다.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public System.Reflection.PropertyInfo? GetProperty (string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.PropertyInfo GetProperty (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags) As PropertyInfo

매개 변수

name
String

가져올 속성의 이름이 포함된 문자열입니다.

bindingAttr
BindingFlags

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

또는

null을 반환하는 Default입니다.

반환

지정된 요구 사항과 일치하는 속성을 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

예외

지정한 이름을 가지며 지정된 바인딩 제약 조건과 일치하는 속성이 둘 이상 발견되었습니다.

name이(가) null인 경우

예제

다음 예제에서는 사용자 정의 클래스의 형식을 검색하고, 해당 클래스의 속성을 검색하고, 지정된 바인딩 제약 조건에 따라 속성 이름을 표시합니다.

using namespace System;
using namespace System::Reflection;
ref class MyClass
{
private:
   int myProperty;

public:

   property int MyProperty 
   {
      // Declare MyProperty.
      int get()
      {
         return myProperty;
      }

      void set( int value )
      {
         myProperty = value;
      }
   }
};

int main()
{
   try
   {
      // Get Type object of MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get the PropertyInfo by passing the property name and specifying the BindingFlags.
      PropertyInfo^ myPropInfo = myType->GetProperty( "MyProperty", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
      
      // Display Name property to console.
      Console::WriteLine( "{0} is a property of MyClass.", myPropInfo->Name );
   }
   catch ( NullReferenceException^ e ) 
   {
      Console::WriteLine( "MyProperty does not exist in MyClass. {0}", e->Message );
   }
}

using System;
using System.Reflection;

class MyClass2
{
    // Declare MyProperty.
    public int MyProperty { get; set; }
}

public class MyTypeClass2
{
    public static void Main(string[] args)
    {
        try
        {
            // Get Type object of MyClass2.
            Type myType = typeof(MyClass2);

            // Get the PropertyInfo by passing the property name and specifying the BindingFlags.
            PropertyInfo myPropInfo = myType.GetProperty(
                "MyProperty",
                BindingFlags.Public | BindingFlags.Instance
                );

            // Display Name property to console.
            Console.WriteLine("{0} is a property of MyClass2.", myPropInfo.Name);
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("MyProperty does not exist in MyClass2." + e.Message);
        }
    }
}
open System
open System.Reflection

type MyClass() =
    let mutable myProperty = 0
    // Declare MyProperty.
    member _.MyProperty
        with get () =
            myProperty
        and set (value) =
            myProperty <- value

try
    // Get Type object of MyClass.
    let myType = typeof<MyClass>
    // Get the PropertyInfo by passing the property name and specifying the BindingFlags.
    let myPropInfo = myType.GetProperty("MyProperty", BindingFlags.Public ||| BindingFlags.Instance)
    // Display Name property to console.
    printfn $"{myPropInfo.Name} is a property of MyClass."
with :? NullReferenceException as e ->
    printfn $"MyProperty does not exist in MyClass.{e.Message}"

Imports System.Reflection
Module Module1
    Public Class MyClass1
        Private myProperty1 As Integer
        ' Declare MyProperty.
        Public Property MyProperty() As Integer
            Get
                Return myProperty1
            End Get
            Set(ByVal Value As Integer)
                myProperty1 = Value
            End Set
        End Property
        Public Shared Sub Main()
            Try
                ' Get a Type object corresponding to MyClass.
                Dim myType As Type = GetType(MyClass1)
                ' Get a PropertyInfo object by passing property name and specifying BindingFlags.
                Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty", BindingFlags.Public Or BindingFlags.Instance)
                ' Display the Name property.
                Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name)
            Catch e As NullReferenceException
                Console.WriteLine("MyProperty does not exist in MyClass.", e.Message.ToString())
            End Try
        End Sub
    End Class
End Module 'Module1

추가 정보

적용 대상

GetProperty(String)

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

지정된 이름의 public 속성을 검색합니다.

public:
 System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public System.Reflection.PropertyInfo? GetProperty (string name);
public System.Reflection.PropertyInfo GetProperty (string name);
member this.GetProperty : string -> System.Reflection.PropertyInfo
abstract member GetProperty : string -> System.Reflection.PropertyInfo
override this.GetProperty : string -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String) As PropertyInfo

매개 변수

name
String

가져올 공용 속성의 이름이 포함된 문자열입니다.

반환

지정된 이름의 공용 속성을 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

예외

지정한 이름을 가진 속성이 둘 이상 있습니다.

name이(가) null인 경우

예제

다음 예제에서는 사용자 정의 클래스의 개체를 검색 Type 하고, 해당 클래스의 속성을 검색하고, 속성 이름을 표시합니다.

using namespace System;
using namespace System::Reflection;
ref class MyClass
{
private:
   int myProperty;

public:

   property int MyProperty 
   {
      // Declare MyProperty.
      int get()
      {
         return myProperty;
      }

      void set( int value )
      {
         myProperty = value;
      }
   }
};

int main()
{
   try
   {
      // Get the Type object corresponding to MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get the PropertyInfo object by passing the property name.
      PropertyInfo^ myPropInfo = myType->GetProperty( "MyProperty" );
      
      // Display the property name.
      Console::WriteLine( "The {0} property exists in MyClass.", myPropInfo->Name );
   }
   catch ( NullReferenceException^ e ) 
   {
      Console::WriteLine( "The property does not exist in MyClass. {0}", e->Message );
   }
}
using System;
using System.Reflection;

class MyClass1
{
    // Declare MyProperty.
    public int MyProperty { get; set; }
}

public class MyTypeClass1
{
    public static void Main(string[] args)
    {
        try
        {
            // Get the Type object corresponding to MyClass1.
            Type myType = typeof(MyClass1);

            // Get the PropertyInfo object by passing the property name.
            PropertyInfo myPropInfo = myType.GetProperty("MyProperty");

            // Display the property name.
            Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.Name);
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("The property does not exist in MyClass1." + e.Message);
        }
    }
}
open System

type MyClass() =
    let mutable myProperty = 0
    // Declare MyProperty.
    member _.MyProperty
        with get () =
            myProperty
        and set (value) = 
            myProperty <- value

try
    // Get the Type object corresponding to MyClass.
    let myType = typeof<MyClass>
    // Get the PropertyInfo object by passing the property name.
    let myPropInfo = myType.GetProperty "MyProperty"
    // Display the property name.
    printfn $"The {myPropInfo.Name} property exists in MyClass."
with :? NullReferenceException as e ->
    printfn $"The property does not exist in MyClass.{e.Message}"
Imports System.Reflection
Class MyClass1
    Private myProperty1 As Integer
    ' Declare MyProperty.

    Public Property MyProperty() As Integer
        Get
            Return myProperty1
        End Get
        Set(ByVal Value As Integer)
            myProperty1 = Value
        End Set
    End Property
End Class

Public Class MyTypeClass
    Public Shared Sub Main(ByVal args() As String)
        Try
            ' Get Type Object corresponding to MyClass.
            Dim myType As Type = GetType(MyClass1)
            ' Get PropertyInfo object by passing property name.
            Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty")
            ' Display Name property to console.
            Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name)
        Catch e As NullReferenceException
            Console.WriteLine("The property does not exist in MyClass.", e.Message.ToString())
        End Try
    End Sub
End Class

설명

이 API에 대한 자세한 내용은 Type.GetProperty에 대한 추가 API 설명을 참조하세요.

추가 정보

적용 대상

GetProperty(String, Type)

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

지정된 이름과 반환 형식의 public 속성을 검색합니다.

public:
 System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType);
member this.GetProperty : string * Type -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type) As PropertyInfo

매개 변수

name
String

가져올 공용 속성의 이름이 포함된 문자열입니다.

returnType
Type

속성의 반환 형식입니다.

반환

지정된 이름의 공용 속성을 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null을 반환합니다.

구현

예외

지정한 이름을 가진 속성이 둘 이상 있습니다.

name이(가) null이거나 returnType이(가) null입니다.

예제

다음 예제에서는 하나의 속성으로 클래스를 정의하고 속성의 이름과 형식을 검색합니다.

using namespace System;
using namespace System::Reflection;
ref class MyClass1
{
private:
   String^ myMessage;

public:

   property String^ MyProperty1 
   {
      String^ get()
      {
         return myMessage;
      }

      void set( String^ value )
      {
         myMessage = value;
      }
   }
};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      
      // Get the PropertyInfo Object* representing MyProperty1.
      PropertyInfo^ myStringProperties1 = myType->GetProperty( "MyProperty1", String::typeid );
      Console::WriteLine( "The name of the first property of MyClass1 is {0}.", myStringProperties1->Name );
      Console::WriteLine( "The type of the first property of MyClass1 is {0}.", myStringProperties1->PropertyType );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException : {0}", e->Message );
   }
   catch ( AmbiguousMatchException^ e ) 
   {
      Console::WriteLine( "AmbiguousMatchException : {0}", e->Message );
   }
   catch ( NullReferenceException^ e ) 
   {
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   //Output:
   //The name of the first property of MyClass1 is MyProperty1.
   //The type of the first property of MyClass1 is System.String.

}

using System;
using System.Reflection;

class MyPropertyTypeClass
{
    public string MyProperty1 { get; set; } = "Hello World.";
}

class TestClass
{
    static void Main()
    {
        try
        {
            Type myType = typeof(MyPropertyTypeClass);

            // Get the PropertyInfo object representing MyProperty1.
            PropertyInfo myStringProperties1 = myType.GetProperty("MyProperty1", typeof(string));

            Console.WriteLine("The name of the first property of MyPropertyTypeClass is {0}.",
                myStringProperties1.Name);
            Console.WriteLine("The type of the first property of MyPropertyTypeClass is {0}.",
                myStringProperties1.PropertyType);
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException :" + e.Message);
        }
        catch (AmbiguousMatchException e)
        {
            Console.WriteLine("AmbiguousMatchException :" + e.Message);
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("Source : {0}", e.Source);
            Console.WriteLine("Message : {0}", e.Message);
        }
        //Output:
        //The name of the first property of MyPropertyTypeClass is MyProperty1.
        //The type of the first property of MyPropertyTypeClass is System.String.
    }
}
open System
open System.Reflection

type MyClass1() =
    let mutable myMessage = "Hello World."
    member _.MyProperty1 
        with get () =
            myMessage
        and set (value) =
            myMessage <- value

try
    let myType = typeof<MyClass1>
    // Get the PropertyInfo object representing MyProperty1.
    let myStringProperties1 = myType.GetProperty("MyProperty1", typeof<string>)
    printfn $"The name of the first property of MyClass1 is {myStringProperties1.Name}."
    printfn $"The type of the first property of MyClass1 is {myStringProperties1.PropertyType}."
with
| :? ArgumentNullException as e ->
    printfn $"ArgumentNullException :{e.Message}"
| :? AmbiguousMatchException as e ->
    printfn $"AmbiguousMatchException :{e.Message}"
| :? NullReferenceException as e ->
    printfn $"Source : {e.Source}"
    printfn $"Message : {e.Message}"
// Output:
//     The name of the first property of MyClass1 is MyProperty1.
//     The type of the first property of MyClass1 is System.String.
Imports System.Reflection
Class MyClass1
    Private myMessage As [String] = "Hello World."
    Public Property MyProperty1() As String
        Get
            Return myMessage
        End Get
        Set(ByVal Value As String)
            myMessage = Value
        End Set
    End Property
End Class

Class TestClass
    Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            ' Get the PropertyInfo object representing MyProperty1. 
            Dim myStringProperties1 As PropertyInfo = myType.GetProperty("MyProperty1", GetType(String))
            Console.WriteLine("The name of the first property of MyClass1 is {0}.", myStringProperties1.Name)
            Console.WriteLine("The type of the first property of MyClass1 is {0}.", myStringProperties1.PropertyType.ToString())
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException :" + e.Message.ToString())
        Catch e As AmbiguousMatchException
            Console.WriteLine("AmbiguousMatchException :" + e.Message.ToString())
        Catch e As NullReferenceException
            Console.WriteLine("Source : {0}", e.Source.ToString())
            Console.WriteLine("Message : {0}", e.Message.ToString())
        End Try
    'Output:
    'The name of the first property of MyClass1 is MyProperty1.
    'The type of the first property of MyClass1 is System.String.

    End Sub
End Class

설명

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

에 대한 name 검색은 대/소문자를 구분합니다. 검색에는 공용 정적 및 공용 instance 속성이 포함됩니다.

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

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

추가 정보

적용 대상