[Mono-list] Crazy generic dictionary?

"Andrés G. Aragoneses [ knocte ] "Andrés G. Aragoneses [ knocte ]
Sun Sep 30 12:21:24 EDT 2007


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:

class mytype
{
   ...
}

class subtypeA : mytype
{
   ...
}


class subtypeB : mytype
{
   ...
}

FooDictionary<mytype> foo = new FooDictionary<mytype>();
foo.Add(new subtypeA());
foo.Add(new subtypeB());

This last addition would remove previous object because we are using 
typeof(mytype) instead of typeof(subtype), am I right?


Besides, what I want to store is a List, not an object (I know, it 
wasn't clear in first email), so my wish would be:

FooDictionary<mytype> foo = new FooDictionary<mytype>();
List<subtypeA> listA = new List<subtypeA>();
List<subtypeB> listB = new List<subtypeB>();
listA.Add(new subtypeA());
listB.Add(new subtypeB());
foo.Add(listA);
foo.Add(listB);

Thanks for your answer,

	Andrés	[ knocte ]

-- 




More information about the Mono-list mailing list