开发者

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

开发者 https://www.devze.com 2023-02-20 09:21 出处:网络
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 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. 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. I'm aware of the GetElementType and GetGenericArguments methods but I can't use them inside I开发者_如何转开发Enumerable<> to make the SequenceEqual work.

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/

Project Description

Perform a deep compare of any two .NET objects using reflection. Shows the differences between the two objects.

Compatiblity

Compatible with .NET Framework 3.5 and higher. 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

NuGet Package

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

0

精彩评论

暂无评论...
验证码 换一张
取 消