add Equality Comparer class to base class for custom property classes in c# -


i'm using concurrentdictionary key made of class public properties.

after playing around code (hashcode on decimal iequalitycomparer in concurrentdictionary) wanted find solution, don't have implement class interface iequalitycomparer every property class make.

i made base class can check whether or not public properties set (or @ least not default value). so, thought, let give try , add iequalitycomparer class base class.

so far, had , work.

using system; using system.collections.concurrent; using system.collections.generic;      private void somemethod() {     concurrentdictionary<twouintsonestringskeyinfo,int> test = new  concurrentdictionary<twouintsonestringskeyinfo, int>(new    twouintsonestringskeyinfo.equalitycomparerelevenuintskeyinfo());     test.tryadd(new twouintsonestringskeyinfo { idone = 1, idtwo = 2, mystring = "hi" }, 1);     test.tryadd(new twouintsonestringskeyinfo { idone = 2, idtwo = 3, mystring = "hello" }, 2);     test.tryadd(new twouintsonestringskeyinfo { idone = 3, idtwo = 4 }, 3);      int result;     test.trygetvalue(new twouintsonestringskeyinfo { idone = 2, idtwo = 3, mystring = "hello" }, out result); }       public class propertybaseclass {     public bool allpublicproperiesareset()     {         //some logic (removed now. not important question)      }      public class equalitycomparerelevenuintskeyinfo : iequalitycomparer<twouintsonestringskeyinfo>     {         public int gethashcode(twouintsonestringskeyinfo obj)         {             int hash = 17;             system.reflection.propertyinfo[] properties = obj.gettype().getproperties().orderby(x=>x.name).toarray();             int counter=0;             foreach(system.reflection.propertyinfo p in properties)             {                 counter++;                 var value = p.getvalue(obj);                 hash = hash * 23 + (value == null ? (math.pow(1.111, counter)).gethashcode() : value.gethashcode());             }              return hash;         }          public bool equals(twouintsonestringskeyinfo x, twouintsonestringskeyinfo y)         {             return x.idone == y.idone &&                     x.idtwo == y.idtwo;         }     } }  public class twouintsonestringskeyinfo : propertybaseclass {     public uint idone { get; set; }     public uint idtwo { get; set; }     public string mystring { get; set; } } 

i made example 3 properties. there more , more different types , don't want make custom class used in concurrentdictionary every property class.

thing (of course), way now, work property classes of type twouintsonestringskeyinfo, because have specify when implementing iequalitycomparer<>.

is there way can implement equalitycomparerelevenuintskeyinfo class base class , making flexible in way equalitycomparerelevenuintskeyinfo checks class implements equalitycomparerelevenuintskeyinfo , use class parameter iequalitycomparer, gethashcode , equals (if can done want, i'll of course change equals method well.

suggestions?

kind regards,

matthijs


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -