It's hard for me to implement a property in C # that only has a getter in an abstract base class, but where do I need to enter setter in one of the derived classes.
Update: for a shorter explanation of a generic example of this question, see this question . The selected answer explained why it is currently not possible to do this in C #, however, in my opinion, a satisfactory solution has not yet been provided.
An overview of my class diagram is shown below:

My goal is that the two TextElementStatic and TextElementReferenceSource classes should have the Text property with both getters and setters, and the TextElementReferenceTarget class should have the Text property with a getter only. I constantly use ITextElement , referring to all these objects, and I need to make sure that there is only a recipient in the ITextElement interface. In addition, the TextElement base class implements a lot of common code, so all classes must inherit from this class.
My current code is as follows:
Interface: ITextElement
public interface ITextElement { string Text { get; } }
Interface: ITextElementUpdatable
public interface ITextElementUpdatable : ITextElement { new string Text { get; set; } }
Abstract class: TextElement (Here my problem is explained below)
public abstract class TextElement : ITextElement {
Abstract class: TextElementUpdatable
public abstract class TextElementUpdatable : TextElement, ITextElementUpdatable {
Class: TextElementStatic
public class TextElementStatic : TextElementUpdatable {
Class: TextElementReferenceSource
public class TextElementReferenceSource : TextElementUpdatable {
Class: TextElementReferenceTarget
public class TextElementReferenceTarget : TextElement {
So my problem is: I really want to declare the Text property in the abstract abstract TextElement of the base class, because it should always be implemented in derived classes (both TextElementUpdatable, TextElementReferenceSource, and TextElementReferenceTarget implements this property). However, if I try to convert the property to public abstract string Text { get; } public abstract string Text { get; } , I get an error in TextElementUpdatable , indicating that
TextElementUpdatable.Text hides the inherited property TextElement.Text
Also, if I change the property in TextElementUpdatable from new to override , the error message is replaced with:
Cannot override because TextElement.Text does not have an overridable set accessor
Now I can go back to TextElement and change the property to public virtual string Text { get; private set; } public virtual string Text { get; private set; } public virtual string Text { get; private set; } and call it day, since this method should never be called in any case (which is basically the solution that I have now), However, if I or someone later create another derived class, I want to force me / they implement the Text property, so I would rather mark it abstract than providing a virtual implementation.
Any suggestions on how I can do this right, even if it requires a lot of refactoring?
I know that I can distinguish two goals for it by providing one inherited Text property using getter only, and then introduce the SetText() method in the ITextElementUpdatable interface. However, I am wondering if a good solution can only be found with properties.
Another similar question, but without any answers, I was able to use: C # - what should I do when each inherited class needs a getter from the base class, and setter is for the ONE inherited class only