Immutable views of mutable types

前端 未结 5 1554
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 10:14

I have a project where I need to construct a fair amount of configuration data before I can execute a process. During the configuration stage, it\'s very convenient to have

5条回答
  •  孤独总比滥情好
    2021-01-05 10:33

    Why can't you provide a separate immutable view of the object?

    public class ImmutableConfiguration {
        private Configuration _config;
        public ImmutableConfiguration(Configuration config) { _config = config; }
        public string Version { get { return _config.Version; } }
    }
    

    or if you don't like the extra typing, make the set members internal rather than public - accessible within the assembly but not by clients of it?

提交回复
热议问题