PropertyInfo.GetGetMethod Method (Boolean)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
When overridden in a derived class, returns the public or non-public get accessor for this property.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- nonPublic
- Type: System.Boolean
true to return a non-public accessor; otherwise, false.
Return Value
Type: System.Reflection.MethodInfoThe get accessor for this property, if nonPublic is true. Returns null if nonPublic is false and the get accessor is non-public, or if nonPublic is true but no get accessor exists.
| Exception | Condition |
|---|---|
| MethodAccessException | Application code attempts to access this member late-bound, for example by using the Type.InvokeMember method. |
This property is the MethodInfo representing the get accessor.
To use the GetGetMethod method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetGetMethod method.
The following example demonstrates both overloads of the GetGetMethod method. The example defines a public property and a protected property. The example uses the GetGetMethod() and GetGetMethod(Boolean) method overloads to display the get accessors of both properties. In the first case, only the get accessor for the public property is displayed.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
using System; using System.Reflection; class Example { // Define properties with different access levels. private string myCaption = "A Default caption"; public string Caption { get { return myCaption; } } private string myText = "Default text."; protected string Text { get { return myText; } } public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Get the PropertyInfo objects. PropertyInfo captionInfo = typeof(Example).GetProperty("Caption"); PropertyInfo textInfo = typeof(Example).GetProperty("Text", BindingFlags.NonPublic | BindingFlags.Instance); outputBlock.Text += "Public get accessors:\n"; // List the public get accessors. MethodInfo[] publicGetAccessors = { captionInfo.GetGetMethod(), textInfo.GetGetMethod() }; foreach (MethodInfo mi in publicGetAccessors) { if (mi == null) { outputBlock.Text += "No get accessor was found.\n"; } else { outputBlock.Text += mi.ToString() + "\n"; } } outputBlock.Text += "\nAll get accessors:\n"; // List all get accessors. MethodInfo[] allGetAccessors = { captionInfo.GetGetMethod(true), textInfo.GetGetMethod(true) }; foreach( MethodInfo mi in allGetAccessors ) { if (mi==null) { outputBlock.Text += "No get accessor was found.\n"; } else { outputBlock.Text += mi.ToString() + "\n"; } } } } /* This example produces the following output: Public get accessors: System.String get_Caption() No get accessor was found. All get accessors: System.String get_Caption() System.String get_Text() */
Note: