C#: interface inheritance getters/setters

后端 未结 4 738
一生所求
一生所求 2020-12-29 04:20

I have a set of interfaces which are used in close conjunction with particular mutable object.

Many users of the object only need the ability to read values from the

4条回答
  •  醉话见心
    2020-12-29 04:58

    One way could be to simply skip the inheritance of the interfaces. Make one read-only interface and one write-only, and implement as necessary:

    interface IBasicPropsReadable {
       int Priority { get; }
       string Name { get; }
    }
    
    interface IBasicPropsWriteable  {
       int Priority { set; }
       string Name { set; }
    }
    
    class SomeClassReadWrite : IBasicPropsReadable, IBasicPropsWriteable {
        int Priority { get; set; }
        string Name { get; set; }
    }
    
    class SomeClassReadOnly : IBasicPropsReadable {
        int Priority { get; }
        string Name { get; }
    }
    

提交回复
热议问题