Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Comparing objects with arbritrary properties.

Reply
 
Old 04-30-2009   #1 (permalink)
Michael D. Ober


 
 

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 SpecsSystem Spec
Old 04-30-2009   #2 (permalink)
Peter Duniho


 
 

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.
Since your question is language-agnostic (and includes an odd, chimeric
approach to pseudo-code ), but specific to the .NET Framework, it more
properly 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 SpecsSystem Spec
Old 05-01-2009   #3 (permalink)
Cor Ligthert[MVP]


 
 

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 SpecsSystem Spec
Old 05-01-2009   #4 (permalink)
Pavel Minaev


 
 

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.
It's useless for his particular scenario, since it won't let you do
"For Each property In Object", or anything like that.
My System SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46