PropertyInfo.GetAccessors PropertyInfo.GetAccessors PropertyInfo.GetAccessors PropertyInfo.GetAccessors Method

定義

このプロパティの get アクセサーと set アクセサーの配列を返します。Returns an array of the get and set accessors on this property.

オーバーロード

GetAccessors() GetAccessors() GetAccessors() GetAccessors()

現在のインスタンスがリフレクションしているプロパティの、パブリックな get アクセサー、および set アクセサーをリフレクションする要素で構成される配列を返します。Returns an array whose elements reflect the public get and set accessors of the property reflected by the current instance.

GetAccessors(Boolean) GetAccessors(Boolean) GetAccessors(Boolean) GetAccessors(Boolean)

現在のインスタンスがリフレクションしているプロパティのパブリックな (指定された場合は非パブリックも) get アクセサー、および set アクセサーをリフレクションする要素で構成される配列を返します。Returns an array whose elements reflect the public and, if specified, non-public get and set accessors of the property reflected by the current instance.

GetAccessors() GetAccessors() GetAccessors() GetAccessors()

現在のインスタンスがリフレクションしているプロパティの、パブリックな get アクセサー、および set アクセサーをリフレクションする要素で構成される配列を返します。Returns an array whose elements reflect the public get and set accessors of the property reflected by the current instance.

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

戻り値

現在のインスタンスがリフレクションしているプロパティの、パブリックな get アクセサー、および set アクセサーをリフレクションする MethodInfo オブジェクトの配列。見つからなかった場合、このメソッドは 0 要素の配列を返します。An array of MethodInfo objects that reflect the public get and set accessors of the property reflected by the current instance, if found; otherwise, this method returns an array with zero (0) elements.

実装

次の例では、 ClassWithProperty.Captionプロパティのパブリックアクセサーを取得し、それらに関する情報を表示します。The following example retrieves the public accessors of the ClassWithProperty.Caption property and displays information about them. また、set アクセスInvoke操作子のメソッドを呼び出してプロパティ値を設定し、getter をプロパティ値を取得します。It also calls the Invoke method of the setter to set the property value and of the getter to retrieve the property value.

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には、次の手順を実行します。To call the GetAccessors method:

  1. クラスをType表すオブジェクトを取得します。Get a Type object that represents the class.

  2. オブジェクトからPropertyInfoオブジェクトを取得します。 TypeFrom the Type object, get the PropertyInfo object.

  3. オブジェクトから、 GetAccessorsメソッドを呼び出します。 PropertyInfoFrom the PropertyInfo object, call the GetAccessors method.

セキュリティ

ReflectionPermission
などの機構をInvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])使用して遅延バインディングが呼び出された場合。when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). MemberAccess (関連する列挙体)Associated enumeration: MemberAccess.

GetAccessors(Boolean) GetAccessors(Boolean) GetAccessors(Boolean) GetAccessors(Boolean)

現在のインスタンスがリフレクションしているプロパティのパブリックな (指定された場合は非パブリックも) get アクセサー、および set アクセサーをリフレクションする要素で構成される配列を返します。Returns an array whose elements reflect the public and, if specified, non-public get and set accessors of the property reflected by the current instance.

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 Boolean Boolean Boolean

返された配列で非パブリック メソッドが返されるかどうかを示します。Indicates whether non-public methods should be returned in the returned array. 非パブリック メソッドを含める場合は true。それ以外の場合は falsetrue if non-public methods are to be included; otherwise, false.

戻り値

現在のインスタンスがリフレクションしているプロパティの、get アクセサー、および set アクセサーをリフレクションする要素で構成される配列。An array whose elements reflect the get and set accessors of the property reflected by the current instance. nonPublictrue の場合、この配列はパブリックと非パブリック両方の get アクセサー、およびset アクセサーを格納します。If nonPublic is true, this array contains public and non-public get and set accessors. nonPublicfalse の場合、この配列はパブリックな get および set アクセサーだけを格納します。If nonPublic is false, this array contains only public get and set accessors. 指定した表示設定でアクセサーが見つからなかった場合、このメソッドは 0 要素の配列を返します。If no accessors with the specified visibility are found, this method returns an array with zero (0) elements.

実装

次の例では、 ClassWithProperty.Captionプロパティのアクセサーを取得し、それらに関する情報を表示します。The following example retrieves the accessors of the ClassWithProperty.Caption property and displays information about them. また、set アクセスInvoke操作子のメソッドを呼び出してプロパティ値を設定し、getter をプロパティ値を取得します。It also calls the Invoke method of the setter to set the property value and of the getter to retrieve the property value.

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には、次の手順を実行します。To call the GetAccessors method:

  1. クラスをType表すオブジェクトを取得します。Get a Type object that represents the class.

  2. オブジェクトからPropertyInfoオブジェクトを取得します。 TypeFrom the Type object, get the PropertyInfo object.

  3. オブジェクトから、 GetAccessorsメソッドを呼び出します。 PropertyInfoFrom the PropertyInfo object, call the GetAccessors method.

セキュリティ

ReflectionPermission
などの機構をInvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])使用して遅延バインディングが呼び出された場合。when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). MemberAccess (関連する列挙体)Associated enumeration: MemberAccess.

適用対象