简体   繁体   English

覆盖C#中具有许多属性的类的等于

[英]Overriding Equals for classes with many properties in C#

I have a number of data classes that have over 25 properties of different value types (and this may change in the future as requirements change). 我有许多数据类,它们具有超过25种不同值类型的属性(随着需求的变化,这可能在将来发生变化)。 I would like to override equals, mostly for unit testing purposes. 我想覆盖equals,主要用于单元测试目的。

Currently, the only way I know how to do this is to actually test for equality of each property hard coded. 目前,我知道如何做到这一点的唯一方法是实际测试硬编码的每个属性的相等性。 This seems bad for two reasons - first, I will have to write a lot of code to test 25 properties for equality - second, if a property in one of the classes is added at a later point in time, the Equals method will not check that, and most likely this will go unnoticed and lead to problems down the road. 这看起来很糟糕有两个原因 - 首先,我将编写大量代码来测试25个属性的相等性 - 第二,如果其中一个类中的属性在稍后的时间点添加,则Equals方法将不会检查这很可能会被忽视并导致问题。

Since Equals usually checks for the properties of classes, there should be a way to dynamically compare the properties of the classes being compared, which ensures that property changes to a class don't result in an incorrect implementation of Equals. 由于Equals通常会检查类的属性,因此应该有一种方法来动态比较要比较的类的属性,这可以确保对类的属性更改不会导致Equals的错误实现。 Is there a way to do this? 有没有办法做到这一点?

you could write something like this using reflection - but this would be very slow. 你可以用反射来写这样的东西 - 但这会很慢。 I would stick with overriding equals but think about which part you really need for equal. 我会坚持用最重要的平等但考虑你真正需要哪一部分是平等的。 I usually only check the immutable parts (like Id) for equality and just ignore the mutable fields and I think this is a good practice. 我通常只检查不可变部分(如Id)是否相等,只是忽略可变字段,我认为这是一个很好的做法。

Try using reflection to compare the properties. 尝试使用反射来比较属性。 See Comparing object properties in c# for more info! 有关详细信息,请参阅比较c#中的对象属性

If your class is an entity, it should have a property which allows you to uniquely identify each instance. 如果您的类是实体,则它应该具有允许您唯一标识每个实例的属性。

If your class is implemented as a value type, you'll have to check for equality by checking each property. 如果您的类被实现为值类型,则必须通过检查每个属性来检查是否相等。 In the latter case, in order to prevent tedious work, you could make use of reflection to get all properties of the class at runtime, retrieve the value and use the TypeDescriptor classes to compare the values. 在后一种情况下,为了防止繁琐的工作,您可以使用反射在运行时获取类的所有属性,检索值并使用TypeDescriptor类来比较值。

You can use some AOP Frameworks . 您可以使用一些AOP框架 If the properties you're goona to compare are much more then those ones you gonna to avoid, mark the properties to skip with special custom attribute. 如果您要比较的属性远远超过您要避免的属性,请使用特殊自定义属性标记要跳过的属性。

Maybe T4 can help you out. 也许T4可以帮到你。 With it you can generate code at a click. 有了它,您只需点击一下即可生成代码。 Within this function you can then use the slow reflection mechanism to create a hard-coded GetHashCode() function that will be called at runtime. 在此函数中,您可以使用慢速反射机制来创建将在运行时调用的硬编码 GetHashCode()函数。 For a first look into T4 take a look at Scotts blog about it. 首先看看T4,看看Scotts的博客 Or simply try searching for Text Template Transformation Toolkit with your favorite search engine. 或者只是尝试使用您最喜欢的搜索引擎搜索Text Template Transformation Toolkit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM