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.CanWrite Property

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

public abstract function get CanWrite () : boolean

Not applicable.

Property Value

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;
 
 // Define one writable property and one not writable.
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
    {
        get{return caption;}
    }
}
  
class Mypropertyinfo
{
    public static int Main()
    {
        Console.WriteLine("\nReflection.PropertyInfo");
  
        // Define two properties.
        Mypropertya Mypropertya = new Mypropertya();
        Mypropertyb Mypropertyb = new Mypropertyb();
  
        // Read and display the property.
        Console.Write("\nMypropertya.Caption = " + Mypropertya.Caption);
        Console.Write("\nMypropertyb.Caption = " + Mypropertyb.Caption);
  
        // Write to the property.
        Mypropertya.Caption = "A- No Change";
        // Mypropertyb.Caption cannot be written to because
        // there is no set accessor.
  
        // Read and display the property.
        Console.Write("\nMypropertya.Caption = " + Mypropertya.Caption);
        Console.Write ("\nMypropertyb.Caption = " + Mypropertyb.Caption);
  
        // 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 CanWrite property.
      
        Console.Write("\nCanWrite a - " + Mypropertyinfoa.CanWrite);
      
        Console.Write("\nCanWrite b - " + Mypropertyinfob.CanWrite);
  
        return 0;
    }
}

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

// Define one writable property and one not writable.
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 String get_Caption()
    {
        return caption ;
    } //get_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();

        // Read and display the property.
        Console.Write(("\nMyPropertyA.Caption = " 
            + myPropertyA.get_Caption()));
        Console.Write(("\nMyPropertyB.Caption = " 
            + myPropertyB.get_Caption()));

        // Write to the property.
        myPropertyA.set_Caption("A- No Change");
        // MyPropertyB.Caption cannot be written to because
        // there is no set accessor.

        // Read and display the property.
        Console.Write(("\nMyPropertyA.Caption = " 
            + myPropertyA.get_Caption()));
        Console.Write(("\nMyPropertyB.Caption = " 
            + myPropertyB.get_Caption()));

        // 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 CanWrite property.
        Console.Write(("\nCanWrite A - " 
            + System.Convert.ToString(myPropertyInfoA.get_CanWrite())));
        Console.Write(("\nCanWrite B - " 
            + System.Convert.ToString(myPropertyInfoB.get_CanWrite())));
     } //main
} //MyPropertyInfo

import System;
import System.Reflection;

//Make two properties, one writable and one not writable 
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 get Caption() : String {
       return caption;
   }
}
 
class Mypropertyinfo
{
   public static function Main() : void
   {
      Console.WriteLine("\nReflection.PropertyInfo");
 
      //Build two properties
      var mypropertya : Mypropertya = new Mypropertya();
      var mypropertyb : Mypropertyb = new Mypropertyb();
 
      //Read and display the property
      Console.Write("\nmypropertya.Caption = " + mypropertya.Caption);
      Console.Write("\nmypropertyb.Caption = " + mypropertyb.Caption);
 
      //Write to the property
      mypropertya.Caption = "A- I have been changed";
      //Note: Mypropertyb.Caption cannot be written as
      // there is no set accessor
 
      //Read and display the property
      Console.Write("\nmypropertya.Caption = " + mypropertya.Caption);
      Console.Write ("\nmypropertyb.Caption = " + mypropertyb.Caption);
 
      //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 CanWrite property
     
      Console.Write("\nCanWrite a - " + Mypropertyinfoa.CanWrite);
     
      Console.Write("\nCanWrite b - " + Mypropertyinfob.CanWrite);
   }
}
Mypropertyinfo.Main();
/*
This code produces the following output:

Reflection.PropertyInfo

mypropertya.Caption = A Default caption
mypropertyb.Caption = B Default caption
mypropertya.Caption = A- I have been changed
mypropertyb.Caption = B Default caption
CanWrite a - true
CanWrite b - false
*/

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2017 Microsoft