共用方式為


PropertyInfo.GetGetMethod Method (Boolean)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

When overridden in a derived class, returns the public or non-public get accessor for this property.

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

Syntax

'Declaration
Public MustOverride Function GetGetMethod ( _
    nonPublic As Boolean _
) As MethodInfo
public abstract MethodInfo GetGetMethod(
    bool nonPublic
)

Parameters

  • nonPublic
    Type: System.Boolean
    true to return a non-public accessor; otherwise, false.

Return Value

Type: System.Reflection.MethodInfo
The get accessor for this property, if nonPublic is true. Returns nulla null reference (Nothing in Visual Basic) if nonPublic is false and the get accessor is non-public, or if nonPublic is true but no get accessor exists.

Exceptions

Exception Condition
MethodAccessException

Application code attempts to access this member late-bound, for example by using the Type.InvokeMember method.

Remarks

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.

Examples

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.

Imports System.Reflection

Class Example

   ' Define properties with different access levels.

   Private myCaption As String = "A Default caption"
   Public ReadOnly Property Caption() As String
      Get
         Return myCaption
      End Get
   End Property

   Private myText As String = "Default text."
   Protected ReadOnly Property Text() As String
      Get
         Return myText
      End Get
   End Property


   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 

      ' Get the PropertyInfo objects.
      Dim captionInfo As PropertyInfo = GetType(Example).GetProperty("Caption")
      Dim textInfo As PropertyInfo = _
         GetType(Example).GetProperty("Text", _
                                      BindingFlags.NonPublic Or BindingFlags.Instance)

      outputBlock.Text &= "Public get accessors:" & vbLf

      ' List the public get accessors.
      Dim publicGetAccessors() As MethodInfo = { captionInfo.GetGetMethod(), _
                                                 textInfo.GetGetMethod() }

      For Each mi As MethodInfo In publicGetAccessors
         If mi Is Nothing Then
            outputBlock.Text &= "No get accessor was found." & vbLf
         Else
            outputBlock.Text &= mi.ToString() & vbLf
         End If
      Next

      outputBlock.Text &= vbLf & "All get accessors:" & vbLf

      ' List all get accessors.
      Dim allGetAccessors() As MethodInfo = { captionInfo.GetGetMethod(True), _
                                              textInfo.GetGetMethod(True) }

      For Each mi As MethodInfo In allGetAccessors
         If mi Is Nothing Then
            outputBlock.Text &= "No get accessor was found." & vbLf
         Else
            outputBlock.Text &= mi.ToString() & vbLf
         End If
      Next

   End Sub
End Class

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.