Availability of Abstract Properties

I have an abstract class with an abstract property for which both Get and Set are set. I know that I will always want to get this property from derived classes, but there are some cases where it makes no sense to set this property in certain types of derived classes.

I can't just omit the Set installation element in a derived class (see code example below). I could override the set of accessors in derived classes so that I don’t do anything with the values ​​passed by the user. But is there another way that a property in a particular derived class is actually read-only? Ultimately, I show these properties in the property grid, and I do not want the user to enter values ​​in a field that will not do anything. Maybe I just pass the property as read only in certain derived classes?

Also, I really would not want to mess with any type of type descriptor to correctly display properties in the property grid, for example, override ICustomTypeDescriptor.

public abstract class MyClass
{
    public abstract string MyProperty
    {
        get;
        set;
    }
}

public abstract class MyDerivedClass
{
    public override string MyProperty
    {
        //VS complains that the Set accessor is missing
        get;
    }
}
+3
3

. , , getter setter , " , ". : " ".

, , getter, , , .

, , , , , , , , , , . .

+3

abstract not override:

public abstract class MyClass
{
    public abstract string MyProperty
    {
        get;
        set;
    }
}

public abstract class MyDerivedClass
{
    public abstract string MyProperty
    {
        get;
    }
}

, @JP, .

0

look like you are looking for the [ReadOnly (true)] attribute this will show the property grid your property as read-only.
but in your class you can use it as a regular property (with read and write capabilities)

0
source

Source: https://habr.com/ru/post/1710033/


All Articles