简体   繁体   English

如何在C#中使用反射来检查集合类型属性的相等性

[英]How to check equality of collection type properties, using reflection in C#

I have a class with many properties, some of them are lists of custom types. 我有一类具有许多属性的类,其中一些是自定义类型的列表。 I need to compare two instances of this class, and get a list of properties that aren't equal in them. 我需要比较该类的两个实例,并获取其中不相等的属性的列表。

I designed it with custom attributes (for the relevant properties that I want to include in the comparison), and using the IEquatable , but for the lists I ran into problems. 我使用自定义属性(针对我想在比较中包括的相关属性)设计了它,并使用IEquatable ,但是对于列表,我遇到了问题。 I was going to use SequenceEqual but it requires the type of the list ( IEnumerable<SomeType> ), which I don't have and don't know how to set. 我打算使用SequenceEqual但是它需要列表的类型( IEnumerable<SomeType> ),我没有该列表,也不知道如何设置。 I'm aware of the GetElementType and GetGenericArguments methods but I can't use them inside IEnumerable<> to make the SequenceEqual work. 我知道GetElementTypeGetGenericArguments方法,但无法在IEnumerable<>使用它们来使SequenceEqual工作。

I'm looking for the best design for this scenario, and also code examples of how to actually do it. 我正在寻找适合这种情况的最佳设计,并且还提供有关如何实际执行此操作的代码示例。

This Codeplex project performs a deep compare of any two .NET objects using reflection: http://comparenetobjects.codeplex.com/ 此Codeplex项目使用反射对任意两个.NET对象进行深入比较: http : //comparenetobjects.codeplex.com/

Project Description 项目描述

Perform a deep compare of any two .NET objects using reflection. 使用反射对任意两个.NET对象进行深层比较。 Shows the differences between the two objects. 显示两个对象之间的差异。

Compatiblity 兼容性

Compatible with .NET Framework 3.5 and higher. 与.NET Framework 3.5和更高版本兼容。 New in 2.0, portable Class Library version works with .NET 4.0+, Silverlight 5+, Windows Phone 8+, Windows RT 8+, Xamarin iOS, and Xamarin Droid 便携式类库版本2.0的新功能,可与.NET 4.0 +,Silverlight 5 +,Windows Phone 8 +,Windows RT 8 +,Xamarin iOS和Xamarin Droid一起使用

NuGet Package NuGet软件包

http://www.nuget.org/packages/CompareNETObjects http://www.nuget.org/packages/CompareNETObjects

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

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