2

Background:

I have 2 instances of an object of the same type. One object is populated with the configuration of a device I'm connected to, the other object is populated with a version of the configuration that I've stored on my hard drive.

The user can alter either, so I'd like to compare them and present the differences to the user.

Each object contains a number of ViewModel properties, all of which extend ViewModelBase, which are the ones I want to compare.

Question:

Is a better way to do this than what I'm about to propose.

I'm thinking of using Reflection to inspect each property in my objects, and for each that extend ViewModelBase, I'll loop through each of those properties. For any that are different, I'll put the name and value into a list and then present that to the user.

Rather than inventing this wheel, I'm wondering if this is this a problem that's been solved before? Is there a better way for it to be done?

DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • 1
    Perhaps see: [Comparing 2 objects and retrieve a list of fields with different values](http://stackoverflow.com/a/3060929) – Marc Gravell Aug 01 '12 at 09:36
  • Hi Marc, I see you stated as a comment "for single items, not bad. Lists are a pain". Unfortunately, ViewModelBase contains a number of `ObservableCollection` lists. Does this negate the utility of your suggestion? – DaveDev Aug 01 '12 at 09:53
  • well, then you're no longer talking about "two objects", so it is a different question to the one you asked ;p But: on that same answer is another post with a codeplex suggestion to do something more complex. – Marc Gravell Aug 01 '12 at 10:05

3 Answers3

3

Depending on the amount of properties to be compared, manual checking would be the more efficient option. However, if you have lots of properties or want the check to be dynamic (i.e. you just add new properties and it automagically works), then I think Reflection is the way to go here.

James
  • 80,725
  • 18
  • 167
  • 237
2

Why not just implement the equals operator for your type?

http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

Edit: Having read more carefully I see what you're actually asking is what the most efficient way of doing the actual comparison is.

Doing it via reflection saves on code but is slower. Doing it with lots of manual comparions is fairly quick but more code.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • This doesn't really answer the question "*Is there a better way for it to be done*" - overriding the `Equals` method doesn't mean you don't have to compare the properties. Although I do agree it's a more appropriate place for the comparison to be made. – James Aug 01 '12 at 09:47
0

If you are fairly determent and lazy in the good way. You can mix benefits of both solutions. With help of tool like cci you can emit method that compares properties. The beauty of this is that your reflection code will be executed on compile time leaving you with strait forward method to execute at runtime. This allows you to change models as you see fit and not worry about comparison code. There is a down side to this and that is learning cci which is quite challenging.

Rafal
  • 12,391
  • 32
  • 54
  • For working at runtime, surely `Reflection.Emit` or `Expression` are saner choices? – Marc Gravell Aug 01 '12 at 10:09
  • Yes, but you will generate code at runtime not compile time this is additional overhead but it is better then reflection. – Rafal Aug 01 '12 at 10:25
  • So store the generated code off in a dynamic delegate. My solution does that to any generated code. – Lee Louviere Aug 01 '12 at 12:08
  • I wouldn't allow this generation to execute more then once per type but it is an overhead on runtime. It all depends on count of types that are to be parsed to generate all methods. If there is few of them then the overhead might be negligible. – Rafal Aug 01 '12 at 12:17