简体   繁体   English

比较 c# 中两个对象的最佳方法是什么

[英]What is the best way to compare two objects in c#

I have two complex objects of the same type.我有两个相同类型的复杂对象。 I want to check if there is any difference between the two of these objects.我想检查这两个对象之间是否有任何区别。 I have two options of achieving this, either by converting both of these objects to string using JsonConvert.SerializeObject and compare the string like我有两种选择来实现这一点,或者通过使用 JsonConvert.SerializeObject 将这两个对象都转换为字符串并比较字符串,例如

var existingData = JsonConvert.SerializeObject(objOld);
var newData = JsonConvert.SerializeObject(objNew);
return existingData == newData;

The other option is to use the reflection and loop through all the properties like below.另一种选择是使用反射并循环遍历所有属性,如下所示。

    protected bool MatchObject(object newObj, object oldObj)
    {
        Type currentType = newObj.GetType();
        PropertyInfo[] props = currentType.GetProperties();

        bool isSameObject = true;
        foreach (var prop in props)
        {
            var i = prop.GetValue(newObj);
            var f = prop.GetValue(oldObj);
            if (!object.Equals(i, f))
            {
                isSameObject = false;
                break;
            }
        }

        return isSameObject;
    }

Which of the above approach is more efficient as per the performance perspective?从性能角度来看,上述哪种方法更有效?

Ugh.啊。 Faced between "serialize it to string and use string compare" and "use reflection" I think I'd tick the "none of the above" box面对“将其序列化为字符串并使用字符串比较”和“使用反射”之间,我想我会勾选“以上都不是”框

Have you considered using a record instead of a class ?您是否考虑过使用record而不是class The compiler writes all the necessary code for you, to determine equality of a record based on the values of its properties, so all that is left for you to do is test your record instances for equality编译器为您编写所有必要的代码,以根据记录的属性值确定记录的相等性,因此您要做的就是测试记录实例的相等性

Alternative ready-to-use option is assertion frameworks.另一种即用型选项是断言框架。 For example 'FluentAssertions' can compare object graph例如 'FluentAssertions' 可以比较 object 图

newObject.Should().BeEquivalentTo(oldObject);

Line above will throw an exception if object and their properties aren't equal.如果 object 及其属性不相等,上述行将引发异常。
Object graph comparison Object 图比较

Notice that this is just another library available via NuGet, so you can use in the "production" code as well;)请注意,这只是另一个可通过 NuGet 获得的库,因此您也可以在“生产”代码中使用;)

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

相关问题 在C#中比较复杂对象的两个列表的最佳方法是什么 - What is the best way to compare two list of complex object in c# C#比较双精度值和一系列值并返回两个文本值的最佳方法是什么 - C# What is the best way to compare a double value to a range of values and return two text values 在 C# 中比较两个 DateTime 的相等性的最佳方法是什么……但只能达到一定的精度? - What's the best way to compare the equality of two DateTime's in C#… but only to a certain precision? C#最好的方式来比较一天中的两个时间 - C# best way to compare two time of the day 比较两个大型列表的最佳方法,C# - Best way to compare two large list, C# 使用C#在运行时合并两个对象的最佳方法是什么? - What is the best way to merge two objects during runtime using C#? 在C#中,将字符串与null进行比较以及“”返回true的最佳方法是什么 - In C#, what is the best way to compare strings with null and “” return true 比较C#中忽略大小写的2个字符的最佳方法是什么? - What is the best way to compare 2 characters ignoring case in C#? 在C#中比较2个整数列表/数组的最佳方法是什么? - What is the best way to compare 2 integer lists / array in C# 比较 c# 中的枚举的最佳和高效方法是什么? - What is the best and performant way to compare enums in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM