PropertyInfo.CanWrite Właściwość

Definicja

Pobiera wartość wskazującą, czy właściwość można zapisać.

public abstract bool CanWrite { get; }

Wartość właściwości

true jeśli ta właściwość może być zapisana w; w przeciwnym razie , false.

Implementuje

Przykłady

W poniższym przykładzie zdefiniowano dwie właściwości. Pierwsza właściwość jest zapisywalna, a CanWrite właściwość to true. Druga właściwość nie jest zapisywalna (nie set ma metody dostępu), a CanWrite właściwość to 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;
    }
}

Uwagi

CanWrite Zwraca true wartość , jeśli właściwość ma metodę set dostępu, nawet jeśli element dostępu to private, internal (lub Friend w Visual Basic) lub protected. Jeśli właściwość nie ma set metody dostępu, metoda zwraca wartość false.

Aby uzyskać wartość CanWrite właściwości:

  1. Type Pobierz obiekt typu, który zawiera właściwość .

  2. Wywołaj metodę PropertyInfo , Type.GetProperty aby uzyskać obiekt reprezentujący właściwość .

  3. Pobierz wartość CanWrite właściwości .

Dotyczy

Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0