相关疑难解决方法(0)

使用NewtonSoft.JSON序列化接口/抽象对象

反序列化接口和抽象属性的一种方法是通过在序列化和反序列化期间将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)

c# serialization json json.net

5
推荐指数
1
解决办法
6316
查看次数

标签 统计

c# ×1

json ×1

json.net ×1

serialization ×1