[Mono-list] Crazy generic dictionary?

Robert Jordan robertj at gmx.net
Sun Sep 30 13:07:18 EDT 2007


Andrés G. Aragoneses [ knocte ] wrote:
> Robert Jordan escribió:
>> Andrés G. Aragoneses [ knocte ] wrote:
>>> Today I've come out with this wish:
>>>
>>> Dictionary<typeof(T), List<T>> InternalCollection;
>>>
>>>
>>> Of course, the compiler tells me an error, but I would want to know if I 
>>> can make this kind of collection with C# generics, in order to obtain 
>>> syntatic sugar and type safety, I mean:
>>>
>>> //compilation should succeed only if oMyObj is TypeX:T
>>> InternalCollection.Add(typeof(TypeX), oMyObj);
>>>
>>> Any thoughts?
>> class FooDictionary<T> : Dictionary<System.Type, T>
>> {
>> 	public void Add (T t)
>> 	{
>> 		this [typeof(T)] = t;
>> 	}
>> }
>>
> 
> But that would only allow adding one object:


Well, then use that slightly modified Add method:

  	public void Add (T t)
  	{
		if (t == null)
			throw new ArgumentNullException ("t");
		this [t.GetType ()] = t;
  	}

Robert



More information about the Mono-list mailing list