First let me explain why I'm using a KeyedCollection. I'm building a DLL and I have a list of items that I need to add to a collection and have them stay in the order I placed them but I also need to access them by both their index and by key (the key is a property of an object which I already defined). If there is any other simpler collection that does this, then please let me know.
Ok now, I need to be able to add items to this collection internally in the DLL but I need it to be publicly available to the DLL's end-user as read-only because I don't want them removing/altering the items I added.
I've searched all over this site, other sites, google in general and I have not been able to find a way to get some sort of read-only KeyedCollection. The closest I came was this page (http://www.koders.com/csharp/fid27249B31BFB645825BD9E0AFEA6A2CCDDAF5A382.aspx?s=keyedcollection#L28) but I couldn't quite get it to work.
UPDATE:
I took a look at those C5 classes. That, along with your other comments, helped me better understand how to create my own read-only class and it seems to work. However I have a problem when I try to cast the regular one to the read-only one. I get a compile-time cannot convert error. Here's the code I created (the first small class is what I originally had):
public class FieldCollection : KeyedCollection<string, Field>
{
protected override string GetKeyForItem(Field field)
{
return field.Name;
}
}
public class ReadOnlyFieldCollection : KeyedCollection<string, Field>
{
protected override string GetKeyForItem(Field field)
{ return field.Name; }
new public void Add(Field field)
{ throw new ReadOnlyCollectionException("This collection is read-only."); }
new public void Clear()
{ throw new ReadOnlyCollectionException("This collection is read-only."); }
new public void Insert(int index, Field field)
{ throw new ReadOnlyCollectionException("This collection is read-only."); }
new public bool Remove(string key)
{ throw new ReadOnlyCollectionException("This collection is read-only."); }
new public bool Remove(Field field)
{ throw new ReadOnlyCollectionException("This collection is read-only."); }
new public bool RemoveAt(int index)
{ throw new ReadOnlyCollectionException("This collection is read-only."); }
}
If I have this variable defined:
private FieldCollection _fields;
then do this:
public ReadOnlyFieldCollection Fields;
Fields = (ReadOnlyFieldCollection)_fields;
it fails to compile. They're both inheriting from the same class, I thought they would be "compatible". How can I cast (or expose) the collection as the read-only type I just created?
I don't know of any built-in solution as well. This is an example of the suggestion I gave on the comment:
public class LockedDictionary : Dictionary<string, string>
{
public override void Add(string key, string value)
{
//do nothing or log it somewhere
//or simply change it to private
}
//and so on to Add* and Remove*
public override string this[string i]
{
get
{
return base[i];
}
private set
{
//...
}
}
}
You'll be able to iterate through the KeyValuePair list and everything else, but it won't be available for writing operations. Just be sure to type it according to your needs.
EDIT
In order to have a sorted list, we can change the base class from Dictionary<T,T>
to a Hashtable
.
It would look like this:
public class LockedKeyCollection : System.Collections.Hashtable
{
public override void Add(object key, object value)
{
//do nothing or throw exception?
}
//and so on to Add* and Remove*
public override object this[object i]
{
get
{
return base[i];
}
set
{
//do nothing or throw exception?
}
}
}
Usage:
LockedKeyCollection aLockedList = new LockedKeyCollection();
foreach (System.Collections.DictionaryEntry entry in aLockedList)
{
//entry.Key
//entry.Value
}
Unfortunately, we can't change the access modifier of the methods, but we can override them to do nothing.