How to diff Property Values of two objects using G

2019-07-09 05:23发布

I have the following classes:

public class Person
{
    public String FirstName { set; get; }
    public String LastName { set; get; }
    public Role Role { set; get; }
}

public class Role
{
    public String Description { set; get; }
    public Double Salary { set; get; }
    public Boolean HasBonus { set; get; }
}

I want to be able to automatically extract the property value diferences between Person1 and Person2, example as below:

public static List<String> DiffObjectsProperties(T a, T b)
{
    List<String> differences = new List<String>();
    foreach (var p in a.GetType().GetProperties())
    {
        var v1 = p.GetValue(a, null);
        var v2 = b.GetType().GetProperty(p.Name).GetValue(b, null);

        /* What happens if property type is a class e.g. Role???
         * How do we extract property values of Role?
         * Need to come up a better way than using .Namespace != "System"
         */
        if (!v1.GetType()
            .Namespace
            .Equals("System", StringComparison.OrdinalIgnoreCase))
            continue;

        //add values to differences List
    }

    return differences;
}

How can I extract property values of Role in Person???

3条回答
看我几分像从前
2楼-- · 2019-07-09 05:40

If the properties aren't value types, why not just call DiffObjectProperties recursively on them and append the result to the current list? Presumably, you'd need to iterate through them and prepend the name of the property in dot-notation so that you could see what is different -- or it may be enough to know that if the list is non-empty the current properties differ.

查看更多
来,给爷笑一个
3楼-- · 2019-07-09 05:43
public static List<String> DiffObjectsProperties(object a, object b)
{
    Type type = a.GetType();
    List<String> differences = new List<String>();
    foreach (PropertyInfo p in type.GetProperties())
    {
        object aValue = p.GetValue(a, null);
        object bValue = p.GetValue(b, null);

        if (p.PropertyType.IsPrimitive || p.PropertyType == typeof(string))
        {
            if (!aValue.Equals(bValue))
                differences.Add(
                    String.Format("{0}:{1}!={2}",p.Name, aValue, bValue)
                );
        }
        else
            differences.AddRange(DiffObjectsProperties(aValue, bValue));
    }

    return differences;
}
查看更多
Animai°情兽
4楼-- · 2019-07-09 05:48

Because I don't know how to tell if:

var v1 = p.GetValue(a, null);

is String FirstName or Role Role. I have been trying to find out how to tell if v1 is a String such as FirstName or a class Role. Therefore I won't know when to recursively pass the object property (Role) back to DiffObjectsProperties to iterate its property values.

查看更多
登录 后发表回答