Immutable views of mutable types

前端 未结 5 1556
隐瞒了意图╮
隐瞒了意图╮ 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:41

    How about:

    struct Readonly
    {
        private T _value;
        private bool _hasValue;
    
        public T Value
        {
            get
            {
                if (!_hasValue)
                    throw new InvalidOperationException();
                return _value;
            }
            set
            {
                if (_hasValue)
                    throw new InvalidOperationException();
                _value = value;
            }
        }
    }
    
    
    [DataContract]
    public sealed class Configuration
    {
        private Readonly _version;
    
        [DataMember]
        public string Version
        {
            get { return _version.Value; }
            set { _version.Value = value; }
        }
    }
    

    I called it Readonly but I'm not sure that's the best name for it though.

提交回复
热议问题