Need to have a type-safe bag of items that all implement a generic interface.
The desire is to do something like:
var stringItem = new IItem<string>();
var numberItem = new IItem<int>();
var items = new List<IItem<T>>(); //T of course doesn't accomplish what I want
items.Add(stringItem);
items.Add(numberItem);
Something like:
interface IItem
{
object Value { get; set; }
}
//Update: 2009-03-19 03:08 PM MST
//Added the following interface for clarity of my question
interface IItem<T> : IItem
{
new T Value { get; set; }
}
Then, I could:
var items = new List<IItem>();
But, I lose type safety in my bag. So, I thought of Dictionary
:
var dict = new Dictionary<Type, List<IItem<T>>>(); //T is wrong again
dict.Add(typeof(string), new List<IItem<string>>); //that sure looks nice