csharp.ReadOnlyDictionary.mustache Maven / Gradle / Ivy
{{>partial_header}}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace {{packageName}}.Client
{
public class ReadOnlyDictionary : IDictionary
{
private IDictionary _dictionaryImplementation;
public IEnumerator> GetEnumerator()
{
return _dictionaryImplementation.GetEnumerator();
}
public ReadOnlyDictionary()
{
_dictionaryImplementation = new Dictionary();
}
public ReadOnlyDictionary(IDictionary dictionaryImplementation)
{
if (dictionaryImplementation == null) throw new ArgumentNullException("dictionaryImplementation");
_dictionaryImplementation = dictionaryImplementation;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) _dictionaryImplementation).GetEnumerator();
}
public void Add(KeyValuePair item)
{
throw new ReadonlyOperationException("This instance is readonly.");
}
public void Clear()
{
throw new ReadonlyOperationException("This instance is readonly.");
}
public bool Contains(KeyValuePair item)
{
return _dictionaryImplementation.Contains(item);
}
public void CopyTo(KeyValuePair[] array, int arrayIndex)
{
_dictionaryImplementation.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair item)
{
throw new ReadonlyOperationException("This instance is readonly.");
}
public int Count
{
get { return _dictionaryImplementation.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public void Add(T key, K value)
{
throw new ReadonlyOperationException("This instance is readonly.");
}
public bool ContainsKey(T key)
{
return _dictionaryImplementation.ContainsKey(key);
}
public bool Remove(T key)
{
throw new ReadonlyOperationException("This instance is readonly.");
}
public bool TryGetValue(T key, out K value)
{
return _dictionaryImplementation.TryGetValue(key, out value);
}
public K this[T key]
{
get { return _dictionaryImplementation[key]; }
set
{
throw new ReadonlyOperationException("This instance is readonly.");
}
}
public ICollection Keys
{
get { return _dictionaryImplementation.Keys; }
}
public ICollection Values
{
get { return _dictionaryImplementation.Values; }
}
}
[Serializable]
public class ReadonlyOperationException : Exception
{
//
// For guidelines regarding the creation of new exception types, see
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
// and
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
//
public ReadonlyOperationException()
{
}
public ReadonlyOperationException(string message) : base(message)
{
}
public ReadonlyOperationException(string message, Exception inner) : base(message, inner)
{
}
protected ReadonlyOperationException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
}
}