PropertyInfo::CanWrite Property
.NET Framework (current version)
Gets a value indicating whether the property can be written to.
Assembly: mscorlib (in mscorlib.dll)
Implements
_PropertyInfo::CanWriteCanWrite returns true if the property has a set accessor, even if the accessor is private, internal (or Friend in Visual Basic), or protected. If the property does not have a set accessor, the method returns false.
To get the value of the CanWrite property:
Get the Type object of the type that includes the property.
Call the Type::GetProperty to get the PropertyInfo object that represents the property.
Retrieve the value of the CanWrite property.
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 namespace System; using namespace System::Reflection; // Define one writable property and one not writable. public ref class Mypropertya { private: String^ caption; public: Mypropertya() : caption( "A Default caption" ) {} property String^ Caption { String^ get() { return caption; } void set( String^ value ) { if ( caption != value ) { caption = value; } } } }; public ref class Mypropertyb { private: String^ caption; public: Mypropertyb() : caption( "B Default caption" ) {} property String^ Caption { String^ get() { return caption; } } }; int main() { Console::WriteLine( "\nReflection.PropertyInfo" ); // Define two properties. Mypropertya^ mypropertya = gcnew Mypropertya; Mypropertyb^ mypropertyb = gcnew Mypropertyb; // Read and display the property. Console::Write( "\nMypropertya->Caption = {0}", mypropertya->Caption ); Console::Write( "\nMypropertyb->Caption = {0}", 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 = {0}", mypropertya->Caption ); Console::Write( "\nMypropertyb->Caption = {0}", 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 - {0}", Mypropertyinfoa->CanWrite ); Console::Write( "\nCanWrite b - {0}", Mypropertyinfob->CanWrite ); return 0; }
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Show: