PropertyInfo.CanWrite Propriété
Définition
Important
Certaines informations concernent des produits en version préliminaire qui peuvent être considérablement modifiés avant leur sortie. Microsoft n’offre aucune garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient une valeur indiquant s'il est possible d'écrire dans la propriété.
public:
abstract property bool CanWrite { bool get(); };
public abstract bool CanWrite { get; }
member this.CanWrite : bool
Public MustOverride ReadOnly Property CanWrite As Boolean
Valeur de la propriété
true s'il est possible d'écrire dans la propriété ; sinon, false.
Implémente
Exemples
L’exemple suivant définit deux propriétés. La première propriété est accessible en écriture et la CanWrite propriété est true. La deuxième propriété n’est pas accessible en écriture (il n’y a pas set d’accesseur), et la CanWrite propriété est 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;
}
}
Imports System.Reflection
' Define one writable property and one not writable.
Public Class Mypropertya
Private myCaption As String = "A Default caption"
Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> value Then
myCaption = value
End If
End Set
End Property
End Class
Public Class Mypropertyb
Private myCaption As String = "B Default caption"
Public ReadOnly Property Caption() As String
Get
Return myCaption
End Get
End Property
End Class
Class Mypropertyinfo
Public Shared Function Main() As Integer
Console.WriteLine(ControlChars.CrLf & "Reflection.PropertyInfo")
' Define two properties.
Dim Mypropertya As New Mypropertya()
Dim Mypropertyb As New Mypropertyb()
' Read and display the property.
Console.Write(ControlChars.CrLf & "Mypropertya.Caption = " & _
Mypropertya.Caption)
Console.Write(ControlChars.CrLf & "Mypropertyb.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(ControlChars.CrLf & "Mypropertya.Caption = " & _
Mypropertya.Caption)
Console.Write(ControlChars.CrLf & "Mypropertyb.Caption = " & _
Mypropertyb.Caption)
' Get the type and PropertyInfo.
Dim MyTypea As Type = Type.GetType("Mypropertya")
Dim Mypropertyinfoa As PropertyInfo = MyTypea.GetProperty("Caption")
Dim MyTypeb As Type = Type.GetType("Mypropertyb")
Dim Mypropertyinfob As PropertyInfo = MyTypeb.GetProperty("Caption")
' Get and display the CanWrite property.
Console.Write(ControlChars.CrLf & "CanWrite a - " & _
Mypropertyinfoa.CanWrite)
Console.Write(ControlChars.CrLf & "CanWrite b - " & _
Mypropertyinfob.CanWrite)
Return 0
End Function
End Class
Remarques
CanWrite retourne true si la propriété a un set accesseur, même si l’accesseur est private, internal (ou Friend en Visual Basic), ou protected. Si la propriété n’a pas d’accesseur set , la méthode retourne false.
Pour obtenir la valeur de la CanWrite propriété :
Obtient l’objet Type du type qui inclut la propriété .
Appelez pour Type.GetProperty obtenir l’objet PropertyInfo qui représente la propriété .
Récupérez la valeur de la CanWrite propriété .