![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Comparing objects with arbritrary properties. Using reflection, how can I compare two objects of the same class. Specifically I have multiple classes that are derived from OpenVMS record structures with nested variants and for auditing purposes, Our VMS system logs any record that is changed before and after the change. In pseudo code here's what I need Collection(of change) CompareObjects<T>(T object1, T object2) { Collection(of change) cc = new Collection(of change) for each public_property pp in object1 if object1.pp <> object2.pp then cc.add(new change(pp.name, object1.pp, object2.pp)) end if next pp return cc } I have never used reflection, but from perusing the help files, it appears this should be doable. A solution in C# or VB is acceptable. Framework is v3.5. Thanks in advance, Mike Ober. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Comparing objects with arbritrary properties. On Thu, 30 Apr 2009 18:17:53 -0700, Michael D. Ober <obermd.@xxxxxx> wrote: Quote: > Using reflection, how can I compare two objects of the same class. > Specifically I have multiple classes that are derived from OpenVMS > record structures with nested variants and for auditing purposes, Our > VMS system logs any record that is changed before and after the change. > In pseudo code here's what I need > > Collection(of change) CompareObjects<T>(T object1, T object2) > { > Collection(of change) cc = new Collection(of change) > > for each public_property pp in object1 > > if object1.pp <> object2.pp then > cc.add(new change(pp.name, object1.pp, object2.pp)) > end if > > next pp > > return cc > } > > I have never used reflection, but from perusing the help files, it > appears this should be doable. A solution in C# or VB is acceptable. > Framework is v3.5. approach to pseudo-code ), but specific to the .NET Framework, it moreproperly belongs in a non-language, .NET-specific newsgroup. I've set the follow-ups accordingly. Please try to avoid inappropriate cross-posts in the future. As for the question, you can use reflection to inspect arbitrary types. But just because you can access a type's members without knowing the type in advance, that doesn't mean you can do anything useful with them. Even in your simple example, it's not at all clear what equality between members would mean. Are you always looking for bit-equality? If so, I hope you don't need to deal with floating point numbers, case-insensitive strings, etc. Are you always looking for reference equality? If so, then what about value types? Floating point numbers will cause a problem there too. Do you want two different strings having the same sequence of characters to be considered equal even if they are two different instances of that sequence of characters? If so, reference equality isn't going to cut it. The fact is, it's very difficult to do a useful comparison between instances of a type without knowing anything in advance about the type. Even "simple" equality isn't necessarily all that simple. _If_ you can come up with a strict definition of what it means for your objects to be "equal", then yes...you surely can use reflection to access the members of the type and apply your definition to them to determine equality. The hard part isn't going to be the reflection...it's going to be coming up with that strict definition of "equal". Even after you've done all that, there remains the question of what sort of data you intend to store in the "change log". I suppose you could just write out all of the reflection information (member type, kind, value, etc.), but your pseudo-code shows writing the property itself. So it's not really clear what you want to log. Oh, and also...keep in mind that reflection is very slow as compared to code that already knows everything it needs to know about a type. Hopefully this approach isn't going to wind up in any performance-critical areas of your code. Pete |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Comparing objects with arbritrary properties. Michael, In addition to Peter, when you have problems to get the right type and you want to do it with Late Binding, then in Visual Basic you have the option strict off. It tries to do for you late binding, Like the last sentence from Peter about reflection, has this the same disadvantages. Cor "Michael D. Ober" <obermd.@xxxxxx> wrote in message news:eaGdnTGS4O5b0GfUnZ2dnUVZ_gGdnZ2d@xxxxxx Quote: > Using reflection, how can I compare two objects of the same class. > Specifically I have multiple classes that are derived from OpenVMS record > structures with nested variants and for auditing purposes, Our VMS system > logs any record that is changed before and after the change. In pseudo > code here's what I need > > Collection(of change) CompareObjects<T>(T object1, T object2) > { > Collection(of change) cc = new Collection(of change) > > for each public_property pp in object1 > > if object1.pp <> object2.pp then > cc.add(new change(pp.name, object1.pp, object2.pp)) > end if > > next pp > > return cc > } > > I have never used reflection, but from perusing the help files, it appears > this should be doable. A solution in C# or VB is acceptable. Framework > is v3.5. > > Thanks in advance, > Mike Ober. > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Comparing objects with arbritrary properties. On Apr 30, 10:39*pm, "Cor Ligthert[MVP]" <Notmyfirstn...@xxxxxx> wrote: Quote: > Michael, > > In addition to Peter, when you have problems to get the right type and you > want to do it with Late Binding, then in Visual Basic you have the option > strict off. "For Each property In Object", or anything like that. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| select-object not returning properties that are objects | PowerShell | |||
| Limited properties with user objects using Get-QADUser | PowerShell | |||
| Using a function to setup objects properties with scope of script | PowerShell | |||
| Info: Can you create your own objects with custom properties. | PowerShell | |||
| Print out arbitrary properties of objects | PowerShell | |||