Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

PropertyInfo.CanRead Property

Gets a value indicating whether the property can be read.

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

public abstract bool CanRead { get; }

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.


using System;
using System.Reflection;

class Example
{
   // Define one readable property and one that is not readable.

   private string _caption = "A Default Caption";
   public string Caption   
   {
      get
      {
         return _caption;
      }
      set
      {
         if (_caption!=value)
         {
            _caption = value;
         }
      }
   }

   private string _text = "Default text";
   public string Text   
   {
      set
      {
         if (_text!=value)
         {
            _text = value;
         }
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "Reflection.PropertyInfo.CanRead\n\n";

      Example ex = new Example();

      outputBlock.Text += "Caption = " + ex.Caption + "\n";
      // 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 + "\n";

      PropertyInfo captionInfo = typeof(Example).GetProperty("Caption");
      PropertyInfo textInfo = typeof(Example).GetProperty("Text");

      // Display the CanRead properties.
      outputBlock.Text += 
         "CanRead for the Caption property: " + captionInfo.CanRead + "\n";
      outputBlock.Text += 
         "CanRead for the Text property: " + textInfo.CanRead + "\n";
   }
}

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


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

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

Community Additions

Show:
© 2017 Microsoft