问题
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