Udostępnij za pośrednictwem


PropertyInfo.GetAccessors Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns an array whose elements reflect the public get, set, and other accessors of the property reflected by the current instance.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function GetAccessors As MethodInfo()
public MethodInfo[] GetAccessors()

Return Value

Type: array<System.Reflection.MethodInfo[]
An array that contains the public get, set, and other accessors of the property reflected by the current instance, if found; otherwise, this method returns an array with 0 (zero) elements.

Exceptions

Exception Condition
MethodAccessException

Application code attempts to access this member late-bound, for example by using the Type.InvokeMember method.

Examples

The following example demonstrates both overloads of the GetAccessors method. The example defines a property with a public get accessor and a protected set accessor. That is, the property can be set only by derived classes. The example uses the GetAccessors() method overload to display the public accessors of the property, and the GetAccessors(Boolean) method overload to display all the accessors of the property.

Imports System.Reflection

Class Example

   ' Define a property that has a public get accessor and a protected set
   ' accessor. That is, the property can only be set by classes that 
   ' derive from Example.
   Private myCaption As String = "A Default caption"
   Public Property Caption() As String
      Get
         Return myCaption
      End Get

      Protected Set(ByVal Value As String)
         If myCaption <> Value Then
            myCaption = Value
         End If
      End Set
   End Property

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 

      ' Get the PropertyInfo.
      Dim captionInfo As PropertyInfo = GetType(Example).GetProperty("Caption")

      outputBlock.Text &= "Public accessors:" & vbLf

      ' List the public accessors.
      For Each mi As MethodInfo In captionInfo.GetAccessors()
         outputBlock.Text &= mi.ToString() & vbLf
      Next

      outputBlock.Text &= vbLf & "All accessors:" & vbLf

      ' List all accessors.
      For Each mi As MethodInfo In captionInfo.GetAccessors(True)
         outputBlock.Text &= mi.ToString() & vbLf
      Next

   End Sub
End Class

' This example produces the following output:
'
'Public accessors:
'System.String get_Caption()
'
'All accessors:
'System.String get_Caption()
'Void set_Caption(System.String)
using System.Reflection;

class Example
{
   // Define a property that has a public get accessor and a protected set
   // accessor. That is, the property can only be set by classes that 
   // derive from Example.
   private string myCaption = "A Default caption";
   public string Caption   
   {
      get
      {
         return myCaption;
      }

      protected set
      {
         if (myCaption!=value)
         {
            myCaption = value;
         }
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Get the PropertyInfo.
      PropertyInfo captionInfo = typeof(Example).GetProperty("Caption");

      outputBlock.Text += "Public accessors:\n";

      // List the public accessors.
      foreach( MethodInfo mi in captionInfo.GetAccessors() )
      {
         outputBlock.Text += mi.ToString() + "\n";
      }

      outputBlock.Text += "\nAll accessors:\n";

      // List all accessors.
      foreach( MethodInfo mi in captionInfo.GetAccessors(true) )
      {
         outputBlock.Text += mi.ToString() + "\n";
      }
   }
}

/* This example produces the following output:

Public accessors:
System.String get_Caption()

All accessors:
System.String get_Caption()
Void set_Caption(System.String)
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.