Is it possible to json-serialize a class with explicitly implemented interface properties?

旧街凉风 提交于 2019-12-13 03:20:42

问题


interface A { string Name {get;set;}}

interface B { string Name {get;set;}}

class C : A, B { string A.Name {get;set;} string B.Name {get;set;}}

var c = new C();
((A)c).Name = "a";
((B)c).Name = "b";

var s = JsonConvert.SerializeObject(c);

The result is an empty json object with no property values. Is it possible to serialize and deserialize such an object?


回答1:


Tell json to serialize private properties:

class C : A, B
{
    [JsonProperty]
    string A.Name { get; set; }
    [JsonProperty]
    string B.Name { get; set; }
}

then your code will produce

{"Application.A.Name":"a","Application.B.Name":"b"}



来源:https://stackoverflow.com/questions/45608948/is-it-possible-to-json-serialize-a-class-with-explicitly-implemented-interface-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!