CanRead Property
Collapse the table of content
Expand the table of content

PropertyInfo.CanRead Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets a value indicating whether the property can be read.

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

'Declaration
Public MustOverride ReadOnly Property CanRead As Boolean

Property Value

Type: System.Boolean
true if this property can be read; otherwise, false.

If the property does not have a get accessor, it cannot be read.

To get the CanRead property, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, get the CanRead value.

The following example defines two properties. The first property is readable, and the CanRead property is true. The second property is not readable (there is no get accessor), and the CanRead property is false.


Imports System.Reflection

Class Example

   ' Define one readable property and one that is not readable.

   Private _caption As String = "A Default Caption"
   Public Property Caption() As String
      Get
         Return _caption 
      End Get
      Set(ByVal Value As String)
         If _caption <> Value Then
            _caption = Value
         End If
      End Set
   End Property

   Private _text As String = "Default text"
   Public WriteOnly Property Text() As String
      Set(ByVal Value As String)
         If _text <> Value Then
            _text = Value
         End If
      End Set
   End Property

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

      outputBlock.Text &= "Reflection.PropertyInfo.CanRead" & vbLf & vbLf

      Dim ex As New Example()

      outputBlock.Text &= "Caption = " & ex.Caption & vbLf
      ' The Text property cannot be read because it has no get accessor. The 
      ' following line of code causes a compile error:
      'outputBlock.Text &= "Text = " & ex.Text & vbLf

      Dim captionInfo As PropertyInfo = GetType(Example).GetProperty("Caption")
      Dim textInfo As PropertyInfo = GetType(Example).GetProperty("Text")

      ' Display the CanRead properties.
      outputBlock.Text &= "CanRead for the Caption property: " & _
         captionInfo.CanRead & vbLf
      outputBlock.Text &= "CanRead for the Text property: " & _
         textInfo.CanRead & vbLf

   End Sub
End Class

' This example produces the following output:
'
'Reflection.PropertyInfo.CanRead
'
'Caption = A Default Caption
'CanRead for the Caption property: True
'CanRead for the Text property: False


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2016 Microsoft