反序列化接口和抽象属性的一种方法是通过在序列化和反序列化期间将TypeNameHandling设置为Auto来实现.但是,当我在直接序列化和反序列化接口对象时尝试相同的操作时,它不起作用 -
interface ISample
{
string Key { get; set; }
}
class A : ISample
{
public string Key { get; set; }
public A(string key)
{
this.Key = key;
}
}
class B : ISample
{
public string Key { get; set; }
public B(string key)
{
this.Key = key;
}
}
Run Code Online (Sandbox Code Playgroud)
序列化和反序列化代码 -
ISample a = new A("keyA");
ISample b = new B("keyB");
var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Auto;
var stringA = JsonConvert.SerializeObject(a, settings);
var …
Run Code Online (Sandbox Code Playgroud)