反序列化接口实例的集合?
我想通过json.net序列化这段代码:
public interface ITestInterface{ string Guid {get;set;}}public class TestClassThatImplementsTestInterface1{ public string Guid { get;set; }}public class TestClassThatImplementsTestInterface2{ public string Guid { get;set; }}public class ClassToSerializeViaJson{ public ClassToSerializeViaJson() { this.CollectionToSerialize = new List<ITestInterface>(); this.CollectionToSerialize.add( new TestClassThatImplementsTestInterface2() ); this.CollectionToSerialize.add( new TestClassThatImplementsTestInterface2() ); } List<ITestInterface> CollectionToSerialize { get;set; }}
我想用json.net序列化/反序列化ClassToSerializeViaJson。序列化工作正常,但反序列化给了我这个错误:
Newtonsoft.Json.JsonSerializationException:无法创建ITestInterface类型的实例。Type是接口或抽象类,无法实例化。
那么我该如何反序列化List<ITestInterface>
集合呢?