How can the following produce an "invalid cast exception" ???
foreach (KeyValuePair<String,Object> entry in HttpRuntime.Cache)
{
if (entry.Value.GetType() == typeof(MyClass))
{
MyClass mc = (MyClass)entry.Value; // ===> Invalid Cast Exception !!!
(To those who are asking why I want to do this : I randomly add and remove a few different objects in/from the cache so the need for this kind of test. I was planning to create some sort of container object that would hold my various objects; I would push only this object in the cache but the question remains : would it solve the cast exception ?)
OK I finally made it.
First of all, my apologies, I was erroneously reading my log file. The exception did not come form
but from
It appears the Cache can't be iterated with a KeyValuePair (a code I had found on the internet...). Once again, sorry for wasting your time searching in a wrong direction... Thanks for your help anyway !
And now for the code that works :