c# - JsonSerializer calling container Equals override method -


first of had around , understand override/virtual etc. haven't found cases specific situation - i'm sure isn't unique. want make sure implementation go right implementation. have following code setup demonstrate issue:

using system; using system.collections.generic; using system.io; using system.text; using newtonsoft.json; using newtonsoft.json.converters;  namespace sandpit {     public class program     {         static void main()         {             var fixture = new fixture                 {                     name = "fixture name",                     participants = new list<participant> {new participant {name = "participant name"}}                 };              var writer = new stringwriter(new stringbuilder());             var serializer = new jsonserializer();             serializer.converters.add(new stringenumconverter());             serializer.serialize(writer, fixture);              console.write(writer.tostring());             console.readkey();         }     }      public class fixture     {         public string name { get; set; }         public list<participant> participants { get; set; }          public override bool equals(object obj)         {             var fixture = (fixture)obj;              return fixture.name == name;         }          public override int gethashcode()         {             return name.gethashcode();         }     }      public class participant     {         public string name { get; set; }          public override bool equals(object obj)         {             var participant = (participant)obj;              return participant.name == name;         }          public override int gethashcode()         {             return name.gethashcode();         }     } } 

now when runs exception on var fixture = (fixture)obj;.

unable cast object of type 'system.collections.generic.list`1[sandpit.participant]' type 'sandpit.fixture'.

i don't understand why getting there. , why breaks correct implementation of overridden object methods.

i know can fix doing public new bool equals(object obj). doing right? these objects integrated application working on, there side effects making change?

many thanks, matt

a small change fixture , participant classes fixes this:

public class fixture  {     public string name { get; set; }     public list<participant> participants { get; set; }      public override bool equals(object obj)     {         var fixture = obj fixture;         return fixture == null ? false : fixture.name == name;     }      public override int gethashcode()     {         return name.gethashcode();     } }  public class participant  {     public string name { get; set; }      public override bool equals(object obj)     {         var participant = obj participant;         return participant == null ? false : participant.name == name;     }      public override int gethashcode()     {         return name.gethashcode();     } } 

if comparing element that's of type, can 2 not equal.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -