PropertyInfo.GetAccessors 메서드

정의

이 속성의 getset 접근자의 배열을 반환합니다.

오버로드

GetAccessors()

현재 인스턴스에서 리플렉션한 속성의 public getset 접근자를 리플렉션하는 요소의 배열을 반환합니다.

GetAccessors(Boolean)

현재 인스턴스에 의해 리플렉션되는 속성의 public 및 non-public(지정된 경우) getset 접근자를 리플렉션하는 요소가 포함된 배열을 반환합니다.

GetAccessors()

Source:
PropertyInfo.cs
Source:
PropertyInfo.cs
Source:
PropertyInfo.cs

현재 인스턴스에서 리플렉션한 속성의 public getset 접근자를 리플렉션하는 요소의 배열을 반환합니다.

public:
 cli::array <System::Reflection::MethodInfo ^> ^ GetAccessors();
public:
 virtual cli::array <System::Reflection::MethodInfo ^> ^ GetAccessors();
public System.Reflection.MethodInfo[] GetAccessors ();
member this.GetAccessors : unit -> System.Reflection.MethodInfo[]
abstract member GetAccessors : unit -> System.Reflection.MethodInfo[]
override this.GetAccessors : unit -> System.Reflection.MethodInfo[]
Public Function GetAccessors () As MethodInfo()

반환

현재 인스턴스에서 리플렉션한 속성의 public getset 접근자를 리플렉션하는 MethodInfo 개체를 반환하거나(있는 경우), 없으면 이 메서드가 요소 개수가 0인 배열을 반환합니다.

구현

예제

다음 예제에서는 속성의 ClassWithProperty.Caption public 접근자를 검색하고 해당 접근자에 대한 정보를 표시합니다. 또한 setter의 메서드를 호출 Invoke 하여 속성 값을 설정하고 getter의 속성을 검색합니다.

using System;
using System.Reflection;
 
// Define a property.
public class ClassWithProperty
{
    private string _caption = "A Default caption";

    public string Caption
    {
        get { return _caption; }
        set { if(_caption != value) _caption = value; }
    }
}
 
class Example
{
    public static void Main()
    {
        ClassWithProperty test = new ClassWithProperty();
        Console.WriteLine("The Caption property: {0}", test.Caption);
        Console.WriteLine("----------");
        // Get the type and PropertyInfo.
        Type t = Type.GetType("ClassWithProperty");
        PropertyInfo propInfo = t.GetProperty("Caption");
 
        // Get the public GetAccessors method.
        MethodInfo[] methInfos = propInfo.GetAccessors();
        Console.WriteLine("There are {0} accessors.",
                          methInfos.Length);
        for(int ctr = 0; ctr < methInfos.Length; ctr++) {
           MethodInfo m = methInfos[ctr];
           Console.WriteLine("Accessor #{0}:", ctr + 1);
           Console.WriteLine("   Name: {0}", m.Name);
           Console.WriteLine("   Visibility: {0}", GetVisibility(m));
           Console.Write("   Property Type: ");
           // Determine if this is the property getter or setter.
           if (m.ReturnType == typeof(void)) {
              Console.WriteLine("Setter");
              Console.WriteLine("   Setting the property value.");
              //  Set the value of the property.
              m.Invoke(test, new object[] { "The Modified Caption" } );
           }
           else {
              Console.WriteLine("Getter");
              // Get the value of the property.
              Console.WriteLine("   Property Value: {0}",
                                m.Invoke(test, new object[] {} ));
           }
        }
        Console.WriteLine("----------");
        Console.WriteLine("The Caption property: {0}", test.Caption);
    }

    static string GetVisibility(MethodInfo m)
    {
       string visibility = "";
       if (m.IsPublic)
          return "Public";
       else if (m.IsPrivate)
          return "Private";
       else
          if (m.IsFamily)
             visibility = "Protected ";
          else if (m.IsAssembly)
             visibility += "Assembly";
       return visibility;
    }
}
// The example displays the following output:
//       The Caption property: A Default caption
//       ----------
//       There are 2 accessors.
//       Accessor #1:
//          Name: get_Caption
//          Visibility: Public
//          Property Type: Getter
//          Property Value: A Default caption
//       Accessor #2:
//          Name: set_Caption
//          Visibility: Public
//          Property Type: Setter
//          Setting the property value.
//       ----------
//       The Caption property: The Modified Caption
Imports System.Reflection

' Define a property.
Public Class ClassWithProperty
    Private _caption As String = "A Default caption"

    Public Property Caption As String
        Get
            Return _caption
        End Get
        Set
            If _caption <> value Then _caption = value
        End Set
    End Property
End Class

Module Example
    Public Sub Main()
        Dim test As New ClassWithProperty()
        Console.WriteLine("The Caption property: {0}", test.Caption)
        Console.WriteLine("----------")
        ' Get the type and PropertyInfo.
        Dim t As Type = Type.GetType("ClassWithProperty")
        Dim propInfo As PropertyInfo = t.GetProperty("Caption")

        ' Get all the accessors.
        Dim methInfos() As MethodInfo = propInfo.GetAccessors()
        Console.WriteLine("There are {0} accessors.",
                          methInfos.Length)
        For ctr As Integer = 0 To methInfos.Length - 1
           Dim m As MethodInfo = methInfos(ctr)
           Console.WriteLine("Accessor #{0}:", ctr + 1)
           Console.WriteLine("   Name: {0}", m.Name)
           Console.WriteLine("   Visibility: {0}", GetVisibility(m))
           Console.Write("   Property Type: ")
           ' Determine if this is the property getter or setter.
''           If (m.ReturnType == typeof(void))
           If m.ReturnType Is GetType(Void) Then
              Console.WriteLine("Setter")
              Console.WriteLine("   Setting the property value.")
              ' Set the value of the property.
              m.Invoke(test, { "The Modified Caption" } )
           Else
              Console.WriteLine("Getter")
              ' Get the value of the property.
              Console.WriteLine("   Property Value: {0}",
                                m.Invoke(test, {} ))
           End If
        Next
        Console.WriteLine("----------")
        Console.WriteLine("The Caption property: {0}", test.Caption)
    End Sub
    
    Private Function GetVisibility(m As MethodInfo) As String
       Dim visibility As String = ""
       If m.IsPublic Then
          Return "Public"
       ElseIf m.IsPrivate Then
          Return "Private"
       Else
          If m.IsFamily Then
             visibility = "Protected "
          ElseIf m.IsAssembly Then
             visibility += "Assembly"
          End If
       End If
       Return visibility
    End Function
End Module
' The example displays the following output:
'       The Caption property: A Default caption
'       ----------
'       There are 2 accessors.
'       Accessor #1:
'          Name: get_Caption
'          Visibility: Public
'          Property Type: Getter
'          Property Value: A Default caption
'       Accessor #2:
'          Name: set_Caption
'          Visibility: Public
'          Property Type: Setter
'          Setting the property value.
'       ----------
'       The Caption property: The Modified Caption

설명

메서드를 호출하려면 다음을 수행 GetAccessors 합니다.

  1. 클래스를 Type 나타내는 개체를 가져옵니다.

  2. 개체에서 Type 개체를 가져옵니다 PropertyInfo .

  3. 개체에서 PropertyInfo 메서드를 호출합니다 GetAccessors .

적용 대상

GetAccessors(Boolean)

Source:
PropertyInfo.cs
Source:
PropertyInfo.cs
Source:
PropertyInfo.cs

현재 인스턴스에 의해 리플렉션되는 속성의 public 및 non-public(지정된 경우) getset 접근자를 리플렉션하는 요소가 포함된 배열을 반환합니다.

public:
 abstract cli::array <System::Reflection::MethodInfo ^> ^ GetAccessors(bool nonPublic);
public abstract System.Reflection.MethodInfo[] GetAccessors (bool nonPublic);
abstract member GetAccessors : bool -> System.Reflection.MethodInfo[]
Public MustOverride Function GetAccessors (nonPublic As Boolean) As MethodInfo()

매개 변수

nonPublic
Boolean

non-public 메서드가 반환된 배열로 반환되어야 하는지 여부를 나타냅니다. non-public 메서드를 포함해야 하는 경우 true이고, 그러지 않으면 false입니다.

반환

현재 인스턴스에 의해 리플렉션되는 속성의 getset 접근자를 리플렉션하는 요소가 포함된 배열입니다. nonPublictrue이면 이 배열에 public 및 non-public getset 접근자가 포함됩니다. nonPublicfalse이면 이 배열에 public getset 접근자만 포함됩니다. 지정된 표시 유형으로 된 접근자가 없으면 요소의 개수가 0인 배열을 반환합니다.

구현

예제

다음 예제에서는 속성의 ClassWithProperty.Caption 접근자를 검색하고 해당 접근자에 대한 정보를 표시합니다. 또한 setter의 메서드를 호출 Invoke 하여 속성 값을 설정하고 getter의 속성을 검색합니다.

using System;
using System.Reflection;

// Define a property.
public class ClassWithProperty
{
    private string _caption = "A Default caption";

    public string Caption
    {
        get { return _caption; }
        set { if(_caption != value) _caption = value; }
    }
}

class Example
{
    public static void Main()
    {
        ClassWithProperty test = new ClassWithProperty();
        Console.WriteLine("The Caption property: {0}", test.Caption);
        Console.WriteLine("----------");
        // Get the type and PropertyInfo.
        Type t = Type.GetType("ClassWithProperty");
        PropertyInfo propInfo = t.GetProperty("Caption");

        // Get the public GetAccessors method.
        MethodInfo[] methInfos = propInfo.GetAccessors(true);
        Console.WriteLine("There are {0} accessors.",
                          methInfos.Length);
        for(int ctr = 0; ctr < methInfos.Length; ctr++) {
           MethodInfo m = methInfos[ctr];
           Console.WriteLine("Accessor #{0}:", ctr + 1);
           Console.WriteLine("   Name: {0}", m.Name);
           Console.WriteLine("   Visibility: {0}", GetVisibility(m));
           Console.Write("   Property Type: ");
           // Determine if this is the property getter or setter.
           if (m.ReturnType == typeof(void)) {
              Console.WriteLine("Setter");
              Console.WriteLine("   Setting the property value.");
              //  Set the value of the property.
              m.Invoke(test, new object[] { "The Modified Caption" } );
           }
           else {
              Console.WriteLine("Getter");
              // Get the value of the property.
              Console.WriteLine("   Property Value: {0}",
                                m.Invoke(test, new object[] {} ));
           }
        }
        Console.WriteLine("----------");
        Console.WriteLine("The Caption property: {0}", test.Caption);
    }

    static string GetVisibility(MethodInfo m)
    {
       string visibility = "";
       if (m.IsPublic)
          return "Public";
       else if (m.IsPrivate)
          return "Private";
       else
          if (m.IsFamily)
             visibility = "Protected ";
          else if (m.IsAssembly)
             visibility += "Assembly";
       return visibility;
    }
}
// The example displays the following output:
//       The Caption property: A Default caption
//       ----------
//       There are 2 accessors.
//       Accessor #1:
//          Name: get_Caption
//          Visibility: Public
//          Property Type: Getter
//          Property Value: A Default caption
//       Accessor #2:
//          Name: set_Caption
//          Visibility: Public
//          Property Type: Setter
//          Setting the property value.
//       ----------
//       The Caption property: The Modified Caption
Imports System.Reflection

' Define a property.
Public Class ClassWithProperty
    Private _caption As String = "A Default caption"

    Public Property Caption As String
        Get
            Return _caption
        End Get
        Set
            If _caption <> value Then _caption = value
        End Set
    End Property
End Class

Module Example
    Public Sub Main()
        Dim test As New ClassWithProperty()
        Console.WriteLine("The Caption property: {0}", test.Caption)
        Console.WriteLine("----------")
        ' Get the type and PropertyInfo.
        Dim t As Type = Type.GetType("ClassWithProperty")
        Dim propInfo As PropertyInfo = t.GetProperty("Caption")

        ' Get all the accessors.
        Dim methInfos() As MethodInfo = propInfo.GetAccessors(True)
        Console.WriteLine("There are {0} accessors.",
                          methInfos.Length)
        For ctr As Integer = 0 To methInfos.Length - 1
           Dim m As MethodInfo = methInfos(ctr)
           Console.WriteLine("Accessor #{0}:", ctr + 1)
           Console.WriteLine("   Name: {0}", m.Name)
           Console.WriteLine("   Visibility: {0}", GetVisibility(m))
           Console.Write("   Property Type: ")
           ' Determine if this is the property getter or setter.
''           If (m.ReturnType == typeof(void))
           If m.ReturnType Is GetType(Void) Then
              Console.WriteLine("Setter")
              Console.WriteLine("   Setting the property value.")
              ' Set the value of the property.
              m.Invoke(test, { "The Modified Caption" } )
           Else
              Console.WriteLine("Getter")
              ' Get the value of the property.
              Console.WriteLine("   Property Value: {0}",
                                m.Invoke(test, {} ))
           End If
        Next
        Console.WriteLine("----------")
        Console.WriteLine("The Caption property: {0}", test.Caption)
    End Sub
    
    Private Function GetVisibility(m As MethodInfo) As String
       Dim visibility As String = ""
       If m.IsPublic Then
          Return "Public"
       ElseIf m.IsPrivate Then
          Return "Private"
       Else
          If m.IsFamily Then
             visibility = "Protected "
          ElseIf m.IsAssembly Then
             visibility += "Assembly"
          End If
       End If
       Return visibility
    End Function
End Module
' The example displays the following output:
'       The Caption property: A Default caption
'       ----------
'       There are 2 accessors.
'       Accessor #1:
'          Name: get_Caption
'          Visibility: Public
'          Property Type: Getter
'          Property Value: A Default caption
'       Accessor #2:
'          Name: set_Caption
'          Visibility: Public
'          Property Type: Setter
'          Setting the property value.
'       ----------
'       The Caption property: The Modified Caption

설명

메서드를 호출하려면 다음을 수행 GetAccessors 합니다.

  1. 클래스를 Type 나타내는 개체를 가져옵니다.

  2. 개체에서 Type 개체를 가져옵니다 PropertyInfo .

  3. 개체에서 PropertyInfo 메서드를 호출합니다 GetAccessors .

적용 대상