views:

307

answers:

4
var listings = new List<FPListing>();

if (Cache["Listings"] == null)
{
    listings = GetFPListings(Industry);
    Cache["Listings"] = listings;
}
else
{
    listings = (List<FPListing>)Cache["Listings"];
}

The cast throws this exception

Unable to cast object of type 'System.Collections.Generic.List1[Listings+FPListing]' to type 'System.Collections.Generic.List1[Listings+FPListing]'.

Which according to GetType are identical types. Is there another step I need to take to get the cast to work?

A: 

Are you sure thats the line thats balking? It may be trying cast in that assignment statement in the if part of the conditional.

If you are talking about the HttpContext.Cache, then you need to add (Cache.Add()) or insert (Cache.Insert()) the item, not store it by index.

Calling "Cache["Listings"] = listings" is trying to retrieve an object with the key "listings"

You also dont have to declare it as new. Try this instead...

List<FPListing>() listings;
StingyJack
Cache["Listings"] = listings; is valid, it's like adding to a dictionary. I tried Cache.Add and I'm getting the same result.
Marshall
Are you still declaring it as a var?
StingyJack
I've defined it explicitly as well. I don't think that should matter though since the types are identical.
Marshall
A: 

I believe the reason is because the compiler is unable to infer the type you are explicitly casting to, even though the compiler knows the type to infer in the original var statement.

Use the "as" keywork instead.

listings = Cache["Listings"] as List<FPListing>();

This is also the safer way of casting as well, as it will return NULL (or default(T)) if it cannot be casted - instead of throwing an exception.

eduncan911
This type of casting is safer only when it's ok to get a null when the key is missing. A weakness with this approach is when there are something in there with the key, but of a different type. Now you can end up with null reference exceptions, when you really wanted an invalid cast exception.
Thomas Eyde
Null works for me as exceptions do not work too well on websites (the bigger picture). :)
eduncan911
+1  A: 

The reason is that the object in the cache was created using a different version of the code, or the same version of the code loaded from a different copy of the dll.

To keep the error from stopping the code, use the as opreator to cast the object. If the cast fails, it will still load the data from the cache:

List<FPListing> listings = Cache["Listings"] as List<FPListing>;

if (listings == null) {
    listings = GetFPListings(Industry);
    Cache["Listings"] = listings;
}
Guffa
Sounds plausible, but how can it happen?
Thomas Eyde
For example if you recompile the code and the cache contains an object from before recompiling.
Guffa
A: 

I suspect that GetFPListings() is returning a different derivative of List, perhaps an IList. The compiler would interpret the var as an IList which would get cached that way, which is not directly convertable to a List.

However, that does not match your exception. So if you copied the exception verbatim, then I have no idea.

Brian Rudolph