PropertyInfo.GetAccessors Method (Boolean)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- nonPublic
- Type: System.Boolean
true to include non-public accessors; otherwise, false.
Return Value
Type: System.Reflection.MethodInfo []An array that contains 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 accessors. If nonPublic is false, this array contains only public accessors. If no accessors with the specified visibility are found, this method returns an array with 0 (zero) elements.
| Exception | Condition |
|---|---|
| MethodAccessException | Application code attempts to access this member late-bound, for example by using the Type.InvokeMember method. |
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.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
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) */
Note: