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.CanWrite 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 written to.

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

public abstract bool CanWrite { get; }

Property Value

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

If the property does not have a set accessor, it cannot be written to.

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

The following example defines two properties. The first property is writable, and the CanWrite property is true. The second property is not writable (there is no set accessor), and the CanWrite property is false.


using System;
using System.Reflection;

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

   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   
   {
      get
      {
         return _text;
      }
   }

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

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

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

/* This example produces the following output:

Reflection.PropertyInfo.CanWrite

CanWrite for the Caption property: True
CanWrite for the Text property: False
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show: