PropertyInfo.CanRead Property
.NET Framework 4
Gets a value indicating whether the property can be read.
Assembly: mscorlib (in mscorlib.dll)
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; } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.