是否可以对具有显式实现的接口属性的类进行json序列化? [英] Is it possible to json-serialize a class with explicitly implemented interface properties?
本文介绍了是否可以对具有显式实现的接口属性的类进行json序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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);
结果是一个没有属性值的空json对象.可以对这样的对象进行序列化和反序列化吗?
The result is an empty json object with no property values. Is it possible to serialize and deserialize such an object?
推荐答案
告诉json序列化私有属性:
Tell json to serialize private properties:
class C : A, B
{
[JsonProperty]
string A.Name { get; set; }
[JsonProperty]
string B.Name { get; set; }
}
然后您的代码将产生
{"Application.A.Name":"a","Application.B.Name":"b"}
{"Application.A.Name":"a","Application.B.Name":"b"}
这篇关于是否可以对具有显式实现的接口属性的类进行json序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文