다음을 통해 공유


PropertyInfo.CanWrite 속성

속성에 쓸 수 있는지 여부를 나타내는 값을 가져옵니다.

네임스페이스: System.Reflection
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public MustOverride ReadOnly Property CanWrite As Boolean
‘사용 방법
Dim instance As PropertyInfo
Dim value As Boolean

value = instance.CanWrite
public abstract bool CanWrite { get; }
public:
virtual property bool CanWrite {
    bool get () abstract;
}
/** @property */
public abstract boolean get_CanWrite ()
public abstract function get CanWrite () : boolean

속성 값

이 속성에 쓸 수 있으면 true이고, 그렇지 않으면 false입니다.

설명

이 속성에 set 접근자가 없으면 쓸 수 없습니다.

CanWrite 속성을 가져오려면 먼저 Type 클래스를 가져옵니다. Type에서 PropertyInfo를 가져옵니다. PropertyInfo에서 CanWrite 값을 가져옵니다.

예제

다음 예제에서는 두 개의 속성을 정의합니다. 첫 번째 속성은 쓸 수 있고 CanWrite 속성은 true입니다. 두 번째 속성은 쓸 수 없고(set 접근자가 없음) CanWrite 속성은 false입니다.

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' 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
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;
    }
}
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;
}
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 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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

PropertyInfo 클래스
PropertyInfo 멤버
System.Reflection 네임스페이스