c# - Calculate changeset for object -
i'm writing windows store app receives object (custom type created) server, lets user edit object , submits "changeset" server. changeset object of same type received object every field set null except fields user edited. fields contain user's edits so:
original case: description: "cracked exhaust pipe" status: "open" customer: "" mileage: 10000 edited case: description: "" status "open" customer: "example inc." mileage: 10000 case changeset: description: "" status: null customer: "example inc." mileage: null
when app first downloads object server make copy of later comparison. user makes changes 1 of objects , when user submits changes, changeset of these 2 objects calculated generic method calculatechangeset
. goes through every property of 2 objects , compares them equality:
public static t calculatechangeset<t>(t oldobject, t newobject) t : new() { t changeset = (t)activator.createinstance(oldobject.gettype()); foreach (propertyinfo property in oldobject.gettype().getruntimeproperties()) { var oldvalue = property.getvalue(oldobject); var newvalue = newobject.gettype().getruntimeproperty(property.name).getvalue(newobject); if (oldvalue != null && newvalue != null) { if (oldvalue ilist) { type listtype = oldvalue.gettype().getruntimeproperty("item").propertytype; ilist<listtype> oldlist = (ilist<listtype>)oldvalue; //visual studio complains ilist<listtype> newlist = (ilist<listtype>)newvalue; //listtype not being found if (!oldlist.sequenceequal(newlist)) { changeset.gettype().getruntimeproperty(property.name).setvalue(changeset, newvalue, null); } } else { if (!oldvalue.equals(newvalue)) { changeset.gettype().getruntimeproperty(property.name).setvalue(changeset, calculatechangeset(oldvalue, newvalue)); } } } else { changeset.gettype().getruntimeproperty(property.name).setvalue(changeset, newvalue); } } return changeset; }
the method works fine every property i've come across except lists created if clause deal lists. since list1.equals(list2)
not compare items in lists, need cast oldvalue
, newvalue
list<t>
able use list.sequenceequal()
.
why error saying the type or namespace name 'listtype' not found (are missing using directive or assembly reference?)
when try use create new lists? if there better way approach problem i'm open suggestions..
try this:
ilist oldlist = (ilist)oldvalue; //visual studio complains ilist newlist = (ilist)newvalue; //listtype not being found if (!nongenericsequenceequal(oldlist, newlist)) { changeset.gettype().getruntimeproperty(property.name).setvalue(changeset, newvalue, null); } public static bool nongenericsequenceequal(ilist a, ilist b) { if (referenceequals(a, b)) { return true; } else if (a == null || b == null) { return false; } else { int count = a.count; if (count != b.count) { return false; } else { (int = 0; < count; i++) { if (!object.equals(a[i], b[i])) { return false; } } return true; } } }
weak code 'object.equals(a[i], b[i])'
another variant via reflection:
if (!(bool)typeof(system.linq.enumerable).getmethod("sequenceequal").makegenericmethod(oldvalue.gettype().getruntimeproperty("item").propertytype).invoke(null, new object[] { oldobject, newobject })) { changeset.gettype().getruntimeproperty(property.name).setvalue(changeset, newvalue, null); }
Comments
Post a Comment