mercredi 7 juin 2017

Accessing a generic dictionary via casting at runtime

Our program has a dictionary of type:

Dictionary<TKey, TCache>();

where TKey and TCache are whatever generic types the encompassing method is given, and TCache is a CacheBase.

In another method I need to reference this object (which is stored in another dictionary where it's stored as an object) and remove a key/value pair from it. At runtime I have the TKey object boxed as an 'object' class.

How would I go about this? I can't cast to Dictionary because this method is not generic and won't know at compile time what type of cache/key it is. These are passed as parameters.

Is there a way to use reflection to cast the dictionary from Dictionary to effectively

Dictionary<CacheBase<key.GetType()>, key.GetType()>

I know this can easily fixed by not using the methods generics and instead making the Dictionary a Dictionary and making CaseBase extend an empty CacheBase, but my supervisor insisted I do it this way





Aucun commentaire:

Enregistrer un commentaire