This documentation is archived and is not being maintained.

PropertyInfo.GetAccessors Method (Boolean)

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

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

public abstract MethodInfo[] GetAccessors(
	bool nonPublic
)

Parameters

nonPublic
Type: System.Boolean
Indicates whether non-public methods should be returned in the MethodInfo array. true if non-public methods are to be included; otherwise, false.

Return Value

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

Implements

_PropertyInfo.GetAccessors(Boolean)

To use the GetAccessors method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetAccessors method.

The following example displays the accessors of the specified property.


using System;
using System.Reflection;

// Define a property.
public class Myproperty   
{
    private string caption = "A Default caption";
    public string Caption
    {
        get{return caption;}
        set {if(caption!=value) {caption = value;}
        }
    }
}

class Mypropertyinfo
{
    public static int Main()
    {
        Console.WriteLine ("\nReflection.PropertyInfo");

        // Get the type and PropertyInfo.
        Type MyType = Type.GetType("Myproperty");
        PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption");

        // Get the public GetAccessors method.
        MethodInfo[] Mymethodinfoarray = Mypropertyinfo.GetAccessors(true);
        Console.Write ("\nThere are "
            + Mymethodinfoarray.Length + " accessors (public).");

        return 0;
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: