Friday 15 March 2013

c# - using reflection to compare object in list -



c# - using reflection to compare object in list -

i have 'invoice' winform c# contains usual textboxes , unbound datagridview. used 'invoice' class object store form fields' values. datagridview rows stored list < subinvoice> properties in 'invoice'.

public static bool compareobjects(object original, object altered) { type o = original.gettype(); type = altered.gettype(); foreach (propertyinfo p in o.getproperties(bindingflags.public | bindingflags.instance)) { console.writeline("original: {0} = {1}", p.name, p.getvalue(original, null)); console.writeline("altered: {0} = {1}", p.name, p.getvalue(altered, null)); if (p.getvalue(original, null).tostring() != p.getvalue(altered, null).tostring()) { //console.writeline("not equal"); homecoming false; } } homecoming true; }

i using global class method uses reflection loop through original 'invoice' object , compared altered 'invoice'. if there difference, user notified save invoice.

the above method works fine properties of 'invoice' class not know how check list in stores value of each datagridrow 'subinvoice' class object stored object in list.

can please give me help here? have checked similar thread in stackoverflow , other forums in vain.

update: need create global generic method check type of classes. can 'invoice', 'customer'. purpose track changes made form @ particular instance , prompt user save.

if whant inform user save pending changes should implement inotifypropertychanged instead of doing black magic.

c# winforms reflection

No comments:

Post a Comment