1

So far, reflection seems to be the only way to dynamically check if two objects are the same. You would do this by iterating through the object's members and checking for which ones contain a different value. However, research has told me that reflection is bad on performance when used this way.

If someone can confirm here whether or not reflection is indeed not a good choice for what I need to do, perhaps there is an alternative?

Note that, whatever route I go, I need the solution to be dynamic. This means no overriding of Equals and then comparing each property one by one. If it's dynamic, I can write one method that will work for all types.

4

1 回答 1

2

反思是一个很好的方法。编写自己的对象散列方法也是如此。如果您编写一个散列方法来查看所有需要比较的属性并从中进行散列,那么它应该是一个非常快的比较。您可能还需要为此使用反射。object.GetHashCode() 的一种覆盖,但不考虑正在查看哪个引用 - 只是值。

尝试一些东西 - 然后决定性能是否是一个问题。

于 2012-06-22T14:22:51.033 回答