Skip to content

Instantly share code, notes, and snippets.

@jakesays-old
Created August 26, 2014 19:27
Show Gist options
  • Save jakesays-old/0a096ea382804139baf7 to your computer and use it in GitHub Desktop.
Save jakesays-old/0a096ea382804139baf7 to your computer and use it in GitHub Desktop.
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