This topic has not yet been rated - Rate this topic

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.5, 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 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.