Created
August 26, 2014 19:27
-
-
Save jakesays-old/0a096ea382804139baf7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
namespace Models.Common.Internal | |
{ | |
public class ReadOnlyState | |
{ | |
public bool IsReadOnly { get; set; } | |
} | |
[Serializable] | |
public struct BackingStore<TStoreType> | |
{ | |
private TStoreType _value; | |
private ReadOnlyState _state; | |
public BackingStore(ReadOnlyState state) | |
{ | |
_value = default(TStoreType); | |
_state = state; | |
} | |
public TStoreType Get() | |
{ | |
return _value; | |
} | |
public void Set(TStoreType value) | |
{ | |
if (_state.IsReadOnly) | |
{ | |
throw new InvalidOperationException(); | |
} | |
_value = value; | |
} | |
} | |
//usage: | |
public class Foo | |
{ | |
private readonly ReadOnlyState _state = new ReadOnlyState(); | |
public void SetReadOnly() | |
{ | |
_state.IsReadOnly = true; | |
} | |
private BackingStore<int> _propOne; | |
private BackingStore<string> _propTwo; | |
public Foo() | |
{ | |
_propOne = new BackingStore<int>(_state); | |
_propTwo = new BackingStore<string>(_state); | |
} | |
public int PropOne | |
{ | |
get { return _propOne.Get(); } | |
set { _propOne.Set(value); } | |
} | |
public string PropTwo | |
{ | |
get { return _propTwo.Get(); } | |
set { _propTwo.Set(value); } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment