Generics in Cache

  • Thread starter Thread starter INeedADip
  • Start date Start date
I

INeedADip

Can I put a generic List<MyObject> in cache?
I read that generics can't be serialized..

Can I cache them? Can I return them from Web Services?
 
Putting something in the cache doesn't involve serializing it. The
cache (and the session and application objects) are just collections
that you're adding to.

Perhaps you're thinking of ViewState, which requires that objects added
to it be serializable because their references aren't added to
ViewState, a serialized copy is added. This allows the object contents
to be stored in the viewstate.

I don't know where you read that generics can't be serialized. Check
this out:

www.devx.com/dotnet/Article/30158/0/page/2

--Bob
 
That's not necessarily true.

If the backing for the session state is ASP State server or sql server,
then yes, serialization is required by the types that you are going to store
there.
 
INeedADip,

Generics can be serialized. However, the requirements for them being
serialized are that the type has to be marked as Serializable (and all the
fields have to be serializable as well).

If your type parameters are stored as fields in your object, then you
have to make sure those types are serializable as well.

Hope this helps.
 
I have a class with List<AnotherClass> as a property.

Then I try to serialize my entire class with:

string ret = string.Empty;
using (MemoryStream ms = new MemoryStream())
{
SoapFormatter sf = new SoapFormatter();
sf.Serialize(ms, this);
ret = System.Text.Encoding.Default.GetString(ms.ToArray());
}
return ret;

And it blows up every time saying it can't serialize generics....
"Serializer does not support serializing Generic Types"


When I said cache I was actually referring to the HttpSessionState. I
thought if we were using StateServer that the objects were serialized to the
server running StateServer?
 
Back
Top