Problem with generics replacing hashtable

W

WT

Hello,

I was using hastable to store collection of objects of the same class
MyClass.
I start replacing this hastable by a Dictionary<string,MyClass>....but
I was storing this hastable in cache an accessing it from another component
in an Provider.
This component was developped in a separate assembly and loaded dynamically.
There was no need for it to know Myclass, it was simply retrieving a
hastable and using the ToString() method on each value.

Now that I store Dictionary<string,MyClass> in cache , I don't know how to
implement this module,
Adding MyClass to it project is not easy due to interrelated classes, I
would have to add lot of classes, and may be place them in a separate
assembly which I don't want.
Is there some way to tell it that what it takes from cache could be casted
to Dictionary<string,object> rather than Dictionary<string,MyClass> ????
or to some general interface used by Dictionary<K,T> that could allow me to
call ToString() on the values ????

This seems to be a general problem, should I go back to my hashtable
implementation ?

Thanks to generic gurus for help.

CS
 
N

Nicholas Paldino [.NET/C# MVP]

CS,

The only way you can do this is if you declare your Dictionary initially
as Dictionary<string, object>. Another option would be to define an
interface for MyClass which has the same methods exposed by MyClass, and
then have your dictionary declared as Dictionary<string, IMyClass>. You can
then define the interface in an assembly that is referenced by both the
calling code, and the assembly that is loaded dyamically.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top