PropertyInfo.GetGetMethod Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns the public get accessor for this property.
Assembly: mscorlib (in mscorlib.dll)
Return Value
Type: System.Reflection.MethodInfoThe public get accessor for this property, if the get accessor exists and is public; otherwise, null.
| Exception | Condition |
|---|---|
| MethodAccessException | Application code attempts to access this member late-bound, for example by using the Type.InvokeMember method. |
This is a convenience method that is equivalent to calling the GetGetMethod(Boolean) method overload with the nonPublic parameter set to false.
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: