两个对象之间的差异在C# [英] differences between two objects in C#

查看:252
本文介绍了两个对象之间的差异在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何找到同一个类的两个对象之间的区别。所以如果我有一个Person类,唯一的区别是Age,它将返回不同的字段。



感谢



如果你选择以后,你必须决定你想深入到对象图中,以确定两个实例是否相同,以及如何你将比较某些基本类型的相等性(例如双精度)。



编写基于反射的差分算法更难,首先 - 我个人直接为需要它的类型(或在帮助类中)实现此功能。


I was wondering how I would find the difference between two objects of the same class. So if I had a Person class with the only difference being Age it will return the field/fields that are different.

Thanks

解决方案

This isn't something that C# (or .NET really) supports directly, however you could implement something by hand for specific types, or write code that uses reflection to diff arbitrary objects.

If you choose the later, you will have to decide how deep you want to go into the object graph to identify whether two instances are identical or not and how you will compare certain primitive types for equality (doubles, for instance).

Writing a reflection-based differencing algorithm is more difficult that it seems at first - personally, I would implement this functionality directly for the types (or within a helper class) where you need it.

这篇关于两个对象之间的差异在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆