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 */
public abstract boolean get_CanRead ()

public abstract function get CanRead () : boolean

Property Value

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;
 
// Define one readable property and one not readable.
public class Mypropertya
{
    private string caption = "A Default caption";
    public string Caption
    {
        get{return caption;}
        set {if(caption!=value) {caption = value;}
        }
    }
}
public class Mypropertyb
{
    private string caption = "B Default caption";
    public string Caption
    {
        set{if(caption!=value) {caption = value;}
        }
    }
}
  
class Mypropertyinfo
{
    public static int Main()
    {
        Console.WriteLine("\nReflection.PropertyInfo");
  
        // Define two properties.
        Mypropertya Mypropertya = new Mypropertya();
        Mypropertyb Mypropertyb = new Mypropertyb();
  
        Console.Write("\nMypropertya.Caption = " + Mypropertya.Caption);
        // Mypropertyb.Caption cannot be read, because
        // there is no get accessor.
  
        // Get the type and PropertyInfo.
        Type MyTypea = Type.GetType("Mypropertya");
        PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption");
        Type MyTypeb = Type.GetType("Mypropertyb");
        PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Caption");
  
        // Get and display the CanRead property.
        Console.Write("\nCanRead a - " + Mypropertyinfoa.CanRead);
        Console.Write("\nCanRead b - " + Mypropertyinfob.CanRead);
  
        return 0;
    }
}

import System.*;
import System.Reflection.*;

// Define one readable property and one not readable.
public class MyPropertyA
{
    private String caption = "A Default caption";

    /** @property 
     */
    public String get_Caption()
    {
        return caption ;
    } //get_Caption

    /** @property 
     */
    public void set_Caption (String value)
    {
        if (caption != value) {
            caption = value;
        }
    } //set_Caption
} //MyPropertyA

public class MyPropertyB
{
    private String caption = "B Default caption";

    /** @property 
     */
    public void set_Caption (String value)
    {
        if ( caption != value  ) {
            caption = value;
        }
    } //set_Caption
} //MyPropertyB

class MyPropertyInfo
{   
    public static void main(String[] args)
    {
        Console.WriteLine("\nReflection.PropertyInfo");

        // Define two properties.
        MyPropertyA myPropertyA =  new MyPropertyA();
        MyPropertyB myPropertyB =  new MyPropertyB();

        Console.Write(("\nMyPropertyA.Caption = " 
            + myPropertyA.get_Caption()));
        // MyPropertyB.Caption cannot be read, because
        // there is no get accessor.
        // Get the type and PropertyInfo.
        Type myTypeA = Type.GetType("MyPropertyA");
        PropertyInfo myPropertyInfoA = myTypeA.GetProperty("Caption");
        Type myTypeB = Type.GetType("MyPropertyB");
        PropertyInfo myPropertyInfoB = myTypeB.GetProperty("Caption");

        // Get and display the CanRead property.
        Console.Write(("\nCanRead A - " 
			+ System.Convert.ToString(myPropertyInfoA.get_CanRead())));
        Console.Write(("\nCanRead B - " 
			+ System.Convert.ToString(myPropertyInfoB.get_CanRead())));
    } //main
} //MyPropertyInfo

import System;
import System.Reflection;
 
//Make two properties, one readable and on not readable
 public class Mypropertya
 {
    private var caption : String = "A Default caption";
    public function get Caption() : String {
        return caption;
    }
    public function set Caption(value:String)
    {
        if(caption!=value) {caption = value;}
    }
 }
 public class Mypropertyb
 {
    private var caption : String = "B Default caption";
    public function set Caption(value:String) {
        if(caption!=value) {caption = value;}
    }
 }
  
 class Mypropertyinfo
 {
    public static function Main() : void
    {
       Console.WriteLine("\nReflection.PropertyInfo");
  
       //Build two properties
       var mypropertya : Mypropertya = new Mypropertya();
       var mypropertyb : Mypropertyb = new Mypropertyb();
  
       Console.Write("\nmypropertya.Caption = " + mypropertya.Caption);
       //Note: Mypropertyb.Caption cannot be read as
       // there is no get accessor
  
       //Get the type and PropertyInfo
       var MyTypea : Type = Type.GetType("Mypropertya");
       var Mypropertyinfoa : PropertyInfo = MyTypea.GetProperty("Caption");
       var MyTypeb : Type = Type.GetType("Mypropertyb");
       var Mypropertyinfob : PropertyInfo = MyTypeb.GetProperty("Caption");
  
       //Get and display the CanRead property
      
       Console.Write("\nCanRead a - " + Mypropertyinfoa.CanRead);
      
       Console.Write("\nCanRead b - " + Mypropertyinfob.CanRead);
  
    }
 }
 Mypropertyinfo.Main();
 /*
 Produces the following output
 
 Reflection.PropertyInfo
 Mypropertya.Caption = A Default caption
 CanRead a - True
 CanRead b - False
 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2017 Microsoft