Re: comparing object values
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Wed, 27 Feb 2008 23:17:41 -0800
On Wed, 27 Feb 2008 22:49:00 -0800, Vikram <Vikram@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Thanks peter,
As per ur recommendation, I m thinking of going with approach 2. But is
there any way, by which I can force developer to implement Object.Equal for
any new property added later on. I mean, if later any new property is added,
of some user defined class type r of type string, then developer has to make
sure that he implemented equals for the new class or modify equals method for
any new property?
Yes, that's true. To some extent, that's just "business as usual". People updating or subclassing a particular class need to be aware of what it's already implementing. Note that even if you were using reflection for this particular purpose, that wouldn't obviate ths need. It would make that particular situation immune, but the class would otherwise still be susceptible.
That said, ironically this is an area where reflection _might_ be useful. As a maintenance aid, you might include some "#if DEBUG" code to the static constructor of the object that uses reflection to inspect the class's properties and the Equals() method. Unfortunately, I don't have the details as to how exactly you'd inspect the Equals() method, but I'm pretty sure there's a way to go through it and see if particular properties in the class are referenced in the method.
In that way, for debug builds, you could cross-reference the Equals() method implementation with the properties that actually exist and put up an assertion failure or throw an exception or whatever if it doesn't match. This would be an acceptable overhead in debug builds (it would only happen once per execution of the application), but would still address the maintenance issue.
You may not find it worth bothering with though. I know that this is not a generally used technique, and yet there are _lots_ of classes out there that override Equals(). Obviously the maintenance overhead and risks related to doing so are far outweighed by the benefits provided.
Pete
.
- References:
- Re: comparing object values
- From: Michael Nemtsev [MVP]
- Re: comparing object values
- From: Michael Nemtsev [MVP]
- Re: comparing object values
- From: Vikram
- Re: comparing object values
- From: Peter Duniho
- Re: comparing object values
- From: Vikram
- Re: comparing object values
- From: Peter Duniho
- Re: comparing object values
- From: Vikram
- Re: comparing object values
- Prev by Date: Impersonate registry user/pwd
- Next by Date: .NET Compatibility
- Previous by thread: Re: comparing object values
- Next by thread: Error messages and xsd generator in english
- Index(es):
Relevant Pages
|
Loading