我想知道我是否可以创建一个返回只读字典的属性?
示例:
private readonly Dictionary<string, IMyObject> _myDictionary; public Dictionary<string, IMyObject> MyDictionary { get { return _myDictionary; } }
因此,不允许使用MyDictionary的人添加,删除或更改项目。有什么方法可以做到这一点?
答案 0 :(得分:7)
我认为你需要一个包裹Dictionary
的课程,就像ReadOnlyCollection
包裹List
一样。
虽然您找不到执行此操作的默认类,但您会在this问题的答案之一中找到实现。
BCL Extras Project也包含这样的实现。它支持创建一个实现IDictionary
的代理对象,并可以在其中使用。
答案 1 :(得分:2)
.Net 4.5:System.Collections.ObjectModel.ReadOnlyDictionary
答案 2 :(得分:1)
继承自System.Collections.ObjectModel.KeyedCollection<TKey,TItem>
覆盖InsertItem
和RemoveItem
答案 3 :(得分:0)
C#并没有像你建议的那样提供这样做的方法,但是你总是可以返回一个包含myDictionary的“自制”不可变字典。
有关创建不可变字典的更多信息,请查看此内容。
答案 4 :(得分:0)
如果提供这个不可变字典的意图是保护你自己的字典,只需给它们一个浅的副本。
public Dictionary<string, IMyObject> MyDictionary
{
get { return new Dictionary<string, IMyObject>(_myDictionary); }
}
来电者可以添加和删除任何内容,但这对您的字典无关紧要。
当然,调用者仍然可以访问字典中的内容并且可以改变它们。如果这是一个问题,请进行深层复制。
答案 5 :(得分:0)
这是我用于所有需要准备获取代码段的人的实现
/// <summary>
/// Read only wrapper for generics based dictionary.
/// Only provides lookup retrieval abilities.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class DictionaryReadOnly<TKey, TValue> : IDictionary<TKey, TValue>
{
#region Private Members
private IDictionary<TKey, TValue> _item;
private bool _throwOnWritableAction = false;
#endregion
#region Constructors
/// <summary>
/// Constructor requiring the generic dictionary being wrapped.
/// </summary>
/// <param name="item"></param>
public DictionaryReadOnly(IDictionary<TKey, TValue> items)
{
_throwOnWritableAction = true;
_item = items;
}
/// <summary>
/// Constructor requiring the generic dictionary being wrapped.
/// </summary>
/// <param name="item"></param>
public DictionaryReadOnly(IDictionary<TKey, TValue> items, bool throwOnWritableAction)
{
_throwOnWritableAction = throwOnWritableAction;
_item = items;
}
#endregion
#region IDictionary<TKey,TValue> Members
/// <summary>
/// Number of items in the dictionary.
/// </summary>
public int Count
{
get { return _item.Count; }
}
/// <summary>
/// Determine if the underlying collection contains the key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsKey(TKey key)
{
return _item.ContainsKey(key);
}
/// <summary>
/// Returns the value associated with the key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public TValue this[TKey key]
{
get { return _item[key]; }
set
{
CheckAndThrow("Set");
}
}
/// <summary>
/// Return keys.
/// </summary>
public ICollection<TKey> Keys
{
get { return _item.Keys; }
}
/// <summary>
/// Not-supported.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void Add(TKey key, TValue value)
{
CheckAndThrow("Add");
}
/// <summary>
/// Not-supported.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Remove(TKey key)
{
CheckAndThrow("Remove");
return false;
}
/// <summary>
/// Try to get the value.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool TryGetValue(TKey key, out TValue value)
{
value = default(TValue);
if (_item.ContainsKey(key))
{
value = _item[key];
return true;
}
return false;
}
/// <summary>
/// Get the values.
/// </summary>
public ICollection<TValue> Values
{
get { return _item.Values; }
}
#endregion
#region ICollection<KeyValuePair<TKey,TValue>> Members
/// <summary>
/// Not-supported.
/// </summary>
/// <param name="item"></param>
public void Add(KeyValuePair<TKey, TValue> item)
{
CheckAndThrow("Add");
}
/// <summary>
/// Not-Supported.
/// </summary>
public void Clear()
{
CheckAndThrow("Clear");
}
/// <summary>
/// Determine whether key value pair is in dictionary.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return _item.Contains(item);
}
/// <summary>
/// Copy items to the array.
/// </summary>
/// <param name="array"></param>
/// <param name="arrayIndex"></param>
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
this._item.CopyTo(array, arrayIndex);
}
/// <summary>
/// Indicate read-only
/// </summary>
public bool IsReadOnly
{
get { return true; }
}
/// <summary>
/// Non-supported action.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(KeyValuePair<TKey, TValue> item)
{
CheckAndThrow("Remove");
return false;
}
#endregion
#region IEnumerable<KeyValuePair<TKey,TValue>> Members
/// <summary>
/// Get the enumerator.
/// </summary>
/// <returns></returns>
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _item.GetEnumerator();
}
#endregion
#region IEnumerable Members
/// <summary>
/// Get the enumerator.
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return _item.GetEnumerator();
}
#endregion
/// <summary>
/// Check and thrown based on flag.
/// </summary>
/// <param name="action"></param>
void CheckAndThrow(string action)
{
if (_throwOnWritableAction)
throw new InvalidOperationException("Can not perform action : " + action + " on this read-only collection.");
}
}