3
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?

Nico
  • 497
  • 1
  • 4
  • 15
  • 1
    Explicit interface implementation results in `private` members. You have to tell json to serialize private properties. You can either add `[JsonProperty]` to private properties/fields you want to serialize or use custom [contract resolver](https://stackoverflow.com/a/24107081/1997232). – Sinatr Aug 10 '17 at 09:28

1 Answers1

5

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"}

Sinatr
  • 20,892
  • 15
  • 90
  • 319