+1 দুর্দান্ত কাজ, টমাস। আমি ReadOnlyD शब्दकोয় আরও একধাপ এগিয়ে নিয়েছি।
অনেক ডেল এর সমাধান মত, আমি সরাতে চেয়েছিলেন Add()
, Clear()
, Remove()
IntelliSense থেকে, ইত্যাদি। তবে আমি চেয়েছিলাম আমার উদ্ভূত বস্তুগুলি প্রয়োগ করা হোক IDictionary<TKey, TValue>
।
তদতিরিক্ত, আমি নীচের কোডটি ভাঙ্গতে চাই: (আবার, ডেলের সমাধান এটিও করে)
ReadOnlyDictionary<int, int> test = new ReadOnlyDictionary<int,int>(new Dictionary<int, int> { { 1, 1} });
test.Add(2, 1); //CS1061
অ্যাড () লাইন ফলাফল এতে:
error CS1061: 'System.Collections.Generic.ReadOnlyDictionary<int,int>' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument
কলার এখনও এটিকে কাস্ট করতে পারে IDictionary<TKey, TValue>
, তবে NotSupportedException
আপনি যদি কেবল পঠনযোগ্য সদস্যদের (টমাসের সমাধান থেকে) ব্যবহার করার চেষ্টা করেন তবে তা উত্থাপিত হবে।
যাইহোক, যে কেউ এর জন্যও আমার সমাধানটি এখানে চেয়েছিলেন:
namespace System.Collections.Generic
{
public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
const string READ_ONLY_ERROR_MESSAGE = "This dictionary is read-only";
protected IDictionary<TKey, TValue> _Dictionary;
public ReadOnlyDictionary()
{
_Dictionary = new Dictionary<TKey, TValue>();
}
public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)
{
_Dictionary = dictionary;
}
public bool ContainsKey(TKey key)
{
return _Dictionary.ContainsKey(key);
}
public ICollection<TKey> Keys
{
get { return _Dictionary.Keys; }
}
public bool TryGetValue(TKey key, out TValue value)
{
return _Dictionary.TryGetValue(key, out value);
}
public ICollection<TValue> Values
{
get { return _Dictionary.Values; }
}
public TValue this[TKey key]
{
get { return _Dictionary[key]; }
set { throw new NotSupportedException(READ_ONLY_ERROR_MESSAGE); }
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return _Dictionary.Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
_Dictionary.CopyTo(array, arrayIndex);
}
public int Count
{
get { return _Dictionary.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _Dictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return (_Dictionary as IEnumerable).GetEnumerator();
}
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
throw new NotSupportedException(READ_ONLY_ERROR_MESSAGE);
}
bool IDictionary<TKey, TValue>.Remove(TKey key)
{
throw new NotSupportedException(READ_ONLY_ERROR_MESSAGE);
}
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
throw new NotSupportedException(READ_ONLY_ERROR_MESSAGE);
}
void ICollection<KeyValuePair<TKey, TValue>>.Clear()
{
throw new NotSupportedException(READ_ONLY_ERROR_MESSAGE);
}
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
throw new NotSupportedException(READ_ONLY_ERROR_MESSAGE);
}
}
}