PropertyInfo.GetAccessors Method (Boolean)


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.

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

public abstract MethodInfo[] GetAccessors(
	bool nonPublic


Type: System.Boolean

Indicates whether non-public methods should be returned in the returned array. true if non-public methods are to be included; otherwise, false.

Return Value

Type: System.Reflection.MethodInfo[]

An array whose elements reflect the get and set accessors of the property reflected by the current instance. If nonPublic is true, this array contains public and non-public get and set accessors. If nonPublic is false, this array contains only public get and set accessors. If no accessors with the specified visibility are found, this method returns an array with zero (0) elements.

To call the GetAccessors method:

  1. Get a Type object that represents the class.

  2. From the Type object, get the PropertyInfo object.

  3. From the PropertyInfo object, call the GetAccessors method.

The following example retrieves the accessors of the ClassWithProperty.Caption property and displays information about them. 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);
        // 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.",
        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("   Setting the property value.");
              //  Set the value of the property.
              m.Invoke(test, new object[] { "The Modified Caption" } );
           else {
              // Get the value of the property.
              Console.WriteLine("   Property Value: {0}",
                                m.Invoke(test, new object[] {} ));
        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";
          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


when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top