[Mono-list] Crazy generic dictionary?

"Andrés G. Aragoneses [ knocte ] "Andrés G. Aragoneses [ knocte ]
Mon Oct 1 13:04:05 EDT 2007


Andrés G. Aragoneses [ knocte ] escribió:
> Robert Jordan escribió:
>> [...]
>> Scratch this. I didn't notice that you were using subclasses.
> 

Finally I've come out with this solution, in case someone is interested:

     class IrmDictionary<T> : Dictionary<Type, IList>
     {
         public void Add<K>(IList<K> t) where K:T
         {
             if (t == null)
             {
                 throw new ArgumentNullException("t");
             }
             if (t.Count > 0)
             {
                 this[t[0].GetType()] = (IList)t;
             }
         }
     }

(I know the 't[0]' seems hackish, but is more readable than obtaining 
the type by reflection; and also I don't need the list if it's empty, so 
I don't mind having to access to the first element.)


Maybe we could have here a possible additional item for the wishlist of 
next C# version:

     class IrmDictionary<T> : Dictionary<Type, IList<K>>
     {
         public void Add<K>(IList<K> t) where K:T
         {
             if (t == null)
             {
                 throw new ArgumentNullException("t");
             }
             if (t.Count > 0)
             {
                 this[t[0].GetType()] = t;
             }
         }
     }

The problem is the relation between the two 'K' letters. I don't if it 
could be feasible to add this feature.

Thanks for all the people replying to this thread. It's been interesting.

Regards,

	Andrés	[ knocte ]

-- 



More information about the Mono-list mailing list