Changing property type in class that implements interface with object type property

前端 未结 3 1420
臣服心动
臣服心动 2021-01-18 16:48

I\'m writing a TemplateEngine that will allow me to use my own markup in text based files. I\'m wanting to add controls as plugins as the application matures. Currently i\'v

3条回答
  •  花落未央
    2021-01-18 17:36

    you could also use generics:

    interface IControl 
    {
        string ID{get;set;}
        T Value{get;set;}
    }
    
    class SomeControl : IControl
    {
        public string ID{get;set}
        public string Value{get;set;}
    }
    
    class SomeOtherControl : IControl
    {
        public string ID{get;set}
        public int Value{get;set;}
    }
    

    I like this better than the explicit interface idea if it's just one return value that needs to change. However, I think if you had several properties that each would return a different type, you wouldn't want to have IControl. At least, I wouldn't. In that case I would recommend the explicit interfaces.

    Of course, this wouldn't work if you didn't have access to the source of IControl.

    Edit: had a typo. Fixed

提交回复
热议问题