PropertyInfo.GetAccessors 方法

定义

返回此属性上的 getset 访问器数组。

重载

GetAccessors()

返回一个数组,其元素反射了由当前实例反射的属性的公共 getset 访问器。

GetAccessors(Boolean)

返回一个数组,其元素反射了当前实例反射的属性的公共及非公共(如果指定)getset 取值函数。

GetAccessors()

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

返回一个数组,其元素反射了由当前实例反射的属性的公共 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()

返回

如果找到访问器,此方法将返回一个 MethodInfo 对象的数组,这些对象反射了由当前实例反射的属性的公共 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();
        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

返回一个数组,其元素反射了当前实例反射的属性的公共及非公共(如果指定)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

指示是否应在返回的数组中返回非公共方法。 如果要包括非公共方法,则为 true;否则为 false

返回

一个数组,其元素反射了当前实例反射的属性的 getset 取值函数。 如果 nonPublictrue,则此数组包含公共及非公共 getset 取值函数。 如果 nonPublicfalse,则此数组仅包含公共 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 方法。

适用于