Windows Dev Center

PropertyInfo.GetGetMethod Method

Returns the public get accessor for this property.

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

'Declaration
Public Function GetGetMethod As MethodInfo

Return Value

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

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.


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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft