PropertyInfo.GetGetMethod Method

May 11, 2014

Returns the public get accessor for this property.

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

public MethodInfo GetGetMethod()

Return Value

Type: System.Reflection.MethodInfo
The public get accessor for this property, if the get accessor exists and is public; otherwise, null.

ExceptionCondition
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.


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()
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft