PropertyInfo.CanWrite 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出是否可寫入屬性。
public:
abstract property bool CanWrite { bool get(); };
public abstract bool CanWrite { get; }
member this.CanWrite : bool
Public MustOverride ReadOnly Property CanWrite As Boolean
屬性值
如果可寫入屬性,則為 true
,否則為 false
。
實作
範例
下列範例會定義兩個屬性。 第一個屬性是可寫入的 CanWrite
,而 屬性為 true
。 第二個屬性無法寫入, (沒有 set
存取子) ,而 CanWrite
屬性為 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;
}
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
備註
CanWrite
true
如果屬性具有set
存取子,即使存取子為 private
、 internal
(或 Friend
Visual Basic) 或 protected
中,也會傳回 。 如果屬性沒有 set
存取子,方法會傳 false
回 。
若要取得 屬性的值 CanWrite :
Type取得包含 屬性之型別的物件。
呼叫 以 Type.GetProperty 取得 PropertyInfo 代表 屬性的物件。
擷取 屬性的值 CanWrite 。