I’m having trouble putting a List<> of an anonymous class into the cache.
var cache = new DataCacheFactory().GetCache("default");
var parts = somethingIQueryable.Select(i => new { i.s1, i.s2 } );
cache.Put("somekey", parts.ToList(), TimeSpan.FromMinutes(2));
This throws a serialization exception. However it works if I put the data in a class like this:
public class A { public string s1, public string s2 }
var cache = new DataCacheFactory().GetCache("default");
var parts = somethingIQueryable.Select(i => new A { s1 = i.s1, s2 = i.s2 } );
cache.Put("somekey", parts.ToList(), TimeSpan.FromMinutes(2));
I would rather not have to define classes for every little bit of data going into the cache though, and was wondering if there is a way to make the first example work?
You will not be able to serialize anonymous types and store them in a cache like this and unfortunately, would need to create
List<A>and store this.This would be because there is nothing to compare the anonymous type against to do the serialization and deserialization. Simply, it has no way of knowing what the anonymous type is, because as it’s name implies, it is anonymous.