// using System.Reflection; // Assume class Example has a property named Property1. Example ex = new Example(); PropertyInfo pi = ex.GetType().GetProperty("Property1"); bool isReadable = pi.CanRead; // true bool isWritable = pi.CanWrite; // true
// using System.Reflection; // Assume class Example has a property named Property2 with only a set accessor. Example ex = new Example(); PropertyInfo pi = ex.GetType().GetProperty("Property2"); bool isReadable = pi.CanRead; // false bool isWritable = pi.CanWrite; // trueIn this example, we create an instance of the Example class and get a reference to its PropertyInfo object named Property2, which only has a set accessor. We then retrieve the values of the CanRead and CanWrite properties of the PropertyInfo object. Since Property2 only has a set accessor, the value of isReadable is set to false, while isWritable is set to true. Package library: System.Runtime.dll