Perhaps you should consider whether to redefine the semantics of your interfaces. I mean, it makes sense to just have a separate write interface in addition to the read-only interface, which you defined as "I."
Here is an example:
interface IReadOnly { int Attr { get; } } interface IWriteOnly { int Attr { set; } } interface I : IReadOnly, IWriteOnly { } class CReadOnly : IReadOnly { protected int _Attr; public int Attr { get { return _Attr; } } } class C : CReadOnly, I { public int Attr { get { return base.Attr; } set { _Attr = value; } } }
EDIT: I changed the receiving part of the C.Attr property as return base.Attr instead of return _Attr; to be more compatible with the sample code in the original question. I also think this is more correct, since you may have more complex logic defined in CReadOnly.Attr that you do not want to duplicate.
Some interfaces and classes have been renamed from the original example. โIโ became โIReadOnlyโ, and โIWithSetโ became โIโ. "C" became "CReadOnly", and "CWithSet" became "C".
This compiled for me without warning.
EDIT: This compiled for me without warnings about interface members. I received 1 warning regarding the class C property of the class hiding the CReadOnly class property, but this should be fully expected, as hiding / shadowing occurs in my example. One strange thing: I can swear that I compiled (and ran!) This code a couple of times and saw 0 warnings. It embarrassed me at that time, but I let it go ... but now I see warning 1, as you would expect. Maybe just a compiler crash.
source share