Can I use a collection initializer on my class which have an Add
method that takes a generic parameter?
My class looks like this:
public class FooCollection : IEnumerable<KeyValuePair<string, Type>>
{
private readonly IDictionary<string, Type> _directory = new Dictionary<string, Type>();
public void Add<T>(string name)
{
_directory.Add(name, typeof(T));
}
public IEnumerator<KeyValuePair<string, Type>> GetEnumerator()
{
return _directory.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _directory.GetEnumerator();
}
}
I can do:
var collection = new FooCollection();
collection.Add<Foo>("Foo");
collection.Add<Bar>("Bar");
collection.Add<Baz>("Baz");
But I would like to use a collection initializer. Can I do that? How?