Partager via


PropertyInfo.CanWrite Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a value indicating whether the property can be written to.

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

Syntax

'Declaration
Public MustOverride ReadOnly Property CanWrite As Boolean
public abstract bool CanWrite { get; }

Property Value

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

Remarks

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.

Examples

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.

Imports System.Reflection

Class Example

   ' Define one writable property and one that is not writable.

   Private _caption As String = "A Default Caption"
   Public Property Caption() As String
      Get
         Return _caption 
      End Get
      Set(ByVal Value As String)
         If _caption <> Value Then
            _caption = Value
         End If
      End Set
   End Property

   Private _text As String = "Default text"
   Public ReadOnly Property Text() As String
      Get
         Return _text 
      End Get
   End Property

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 

      outputBlock.Text &= "Reflection.PropertyInfo.CanWrite" & vbLf & vbLf

      Dim captionInfo As PropertyInfo = GetType(Example).GetProperty("Caption")
      Dim textInfo As PropertyInfo = GetType(Example).GetProperty("Text")

      ' Display the CanWrite properties.
      outputBlock.Text &= "CanWrite for the Caption property: " & _
         captionInfo.CanWrite & vbLf
      outputBlock.Text &= "CanWrite for the Text property: " & _
         textInfo.CanWrite & vbLf

   End Sub
End Class

' This example produces the following output:
'
'Reflection.PropertyInfo.CanWrite
'
'CanWrite for the Caption property: True
'CanWrite for the Text property: 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
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.