I am using C# StackExchange.Redis cache, and am attempting to cache an object that has a collection of interface objects. Cache works fine, but deserialization throws the error:
Could not create an instance of type IMyInterface. Type is an interface or abstract class and cannot be instantiated.
If I were handling the deserialization myself, I understand I could create custom deserialization rules via this SO post:
var obj = JsonConvert.DeserializeObject <MyParentClass>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Binder = binder // write some custom deserialization rules
});
However, I don't have access to the deserialization process because it handled by Redis / Newtonsoft.Json. Btw, I am using StackExchange.Redis 1.1.0.0 and Newtonsoft.Json 9.0.0.0
I also found this SO post, about specifying a serialVersionUID
, however this is explicitly for Java as outlined in this SO post.
So what am I missing or what are my options? Thanks in advance!
Here is sample code to work with:
public interface IMyInterface
{
string Name { get; set; }
}
public class MyChildClassA : IMyInterface
{
public string Name { get; set; }
}
public class MyChildClassB : IMyInterface
{
public string Name { get; set; }
}
public class MyParentClass
{
public IList<IMyInterface> Children { get; set; }
// Assume proper init of list in constructor
}
public void DoStuff()
{
var parentValue = new MyParentClass();
// Assume parentValue.Children contains objects of both MyChildClassA and MyChildClassB
// Works!
MyCacheImplementation.Add<MyParentClass>(someKey, parentValue);
// Does NOT work!
MyCacheImplementation.Get<MyParentClass>(someKey);
}