简体   繁体   English

在C#中发现两个对象之间的差异

[英]Find difference between two objects in C#

I'm looking for a way to find the difference between the values of properties between two objects of the same type. 我正在寻找一种方法来查找相同类型的两个对象之间的属性值之间的差异。

I've been working from the example in this stack overflow question: 我一直在研究此堆栈溢出问题中的示例:

Finding property differences between two C# objects 查找两个C#对象之间的属性差异

This is what I have so far: 这是我到目前为止的内容:

public static string Compaire<T>(T initial, T final)
{
    Type currentType = initial.GetType();
    PropertyInfo[] props = currentType.GetProperties();
    StringBuilder sb = new StringBuilder();

    foreach (var prop in props)
    {

        Type equatable = prop.PropertyType.GetInterface("System.IEquatable");

        if (equatable != null)
        {
            var i = prop.GetValue(initial);
            var f = prop.GetValue(final);

            if (i!= null && f != null && !i.Equals(f))
            {
                sb.Append(String.Format(_"{0}.{1} has changed from {2} to {3}. ", currentType.BaseType.Name, prop.Name, i, f));
            }
        }

    }
    return sb.ToString();
}

This is working for most cases however, nullable properties (like Nullable int for example) are not getting compared because (I think) they are not being unboxed and nullable isn't implemented with IEquatable. 在大多数情况下,这是可行的,但由于(我认为)它们没有被取消装箱并且不能用IEquatable实现,因此无法比较可空属性(例如Nullable int )。

Using this method of reflection, is it possible to compare nullables while still avoiding entities that have been disposed (eg "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection")? 使用这种反射方法,是否有可能比较可为空的对象,同时又避免已被放置的实体(例如“ ObjectContext实例已被放置并且不能再用于需要连接的操作”)?

You could use object.Equals(i,f) and omit the check for IEquatable. 您可以使用object.Equals(i,f)并忽略IEquatable的检查。 If it is neccessary but you would like to include nullables you could include them the following way: 如果有必要,但您想包括可为空的内容,则可以通过以下方式包括它们:

        if (prop.PropertyType.IsGenericType)
        {
            if (prop.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                Type typeParameter = prop.PropertyType.GetGenericArguments()[0];

                var i = prop.GetValue(initial);
                var f = prop.GetValue(final);  
                if(object.Equals(i,f))
                {
                    //...
                }
            }
        }

So you check explicitly for Nullables. 因此,您明确检查Nullable。 The first line checks if the type is generic (true for Nullable List etc.) The second gets the underlying generic type (Nullable<>, List<>) and compares it with the Nullable<> type. 第一行检查类型是否为泛型(对于Nullable List等为true)。第二行获取基础泛型类型(Nullable <>,List <>),并将其与Nullable <>类型进行比较。 The third line gets the first (and in case of Nullable<> only) parameter, just for the case you are interested in what type the Nullable is. 第三行获取第一个参数(并且仅在Nullable <>的情况下),仅针对您对Nullable的类型感兴趣的情况。 So you will get int for Nullable. 因此,您将为Nullable获得int。 It is not needed in the code I written, but perhaps you are interested in it. 我编写的代码中不需要它,但是也许您对此感兴趣。 Then the values of both properties are read and compared. 然后读取和比较这两个属性的值。

也许这个项目可以满足您的需求: CompareNetObjects

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

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