使用反射比较对象属性

问题描述 投票:0回答:6

我有两个类地址和员工如下:

 public class Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

   public class Employee
    {
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public Address EmployeeAddress { get; set; }
    }

我有两个员工实例,如下:

    var emp1Address = new Address();
    emp1Address.AddressLine1 = "Microsoft Corporation";
    emp1Address.AddressLine2 = "One Microsoft Way";
    emp1Address.City = "Redmond";
    emp1Address.State = "WA";
    emp1Address.Zip = "98052-6399";

    var emp1 = new Employee();
    emp1.FirstName = "Bill";
    emp1.LastName = "Gates";
    emp1.EmployeeAddress = emp1Address;


    var emp2Address = new Address();
    emp2Address.AddressLine1 = "Gates Foundation";
    emp2Address.AddressLine2 = "One Microsoft Way";
    emp2Address.City = "Redmond";
    emp2Address.State = "WA";
    emp2Address.Zip = "98052-6399";

    var emp2 = new Employee();
    emp2.FirstName = "Melinda";
    emp2.LastName = "Gates";
    emp2.EmployeeAddress = emp2Address;

现在我如何编写一个方法来比较这两个员工并返回具有不同值的属性列表。所以在这个例子中我希望结果是 FirstName 和 Address.AddressLine1 。

c# asp.net reflection
6个回答
30
投票

就像 LBushskin 所说,你不必这样做。这不是最快的方法!如果你想买,试试这个:

    public static List<PropertyInfo> GetDifferences(Employee test1, Employee test2)
    {
        List<PropertyInfo> differences = new List<PropertyInfo>();
        foreach (PropertyInfo property in test1.GetType().GetProperties())
        {
            object value1 = property.GetValue(test1, null);
            object value2 = property.GetValue(test2, null);
            if (!value1.Equals(value2))
            {
                differences.Add(property);
            }
        }
        return differences;
    }

9
投票

您不一定需要反射来执行比较。您可以编写一个比较器类,它接受 Employee 或 Address 的两个实例,并比较每个应该匹配的字段。对于任何不匹配的内容,您可以将字符串(或

PropertyInfo
)元素添加到某个列表中以返回给调用者。

是否返回

PropertyInfo
MemberInfo
,或者只是一个字符串,取决于调用者需要对结果执行什么操作。如果您确实需要访问包含差异的字段,那么
PropertyInfo/MemberInfo
可能会更好 - 但仅报告差异,字符串可能就足够了。

反射的主要价值是编写一个“通用”对象比较器,它可以采用“任何类型的对象”的两个实例并比较它们的公共字段和属性。这有助于避免一遍又一遍地编写重复的比较代码 - 但这看起来不像你所处的情况。 这是一个基于 Oskar Kjellin 的

awnser

7
投票

我也将此代码发布为gist,因此您可以检查最新版本或星号/克隆/分叉它:)

using System; using System.Collections.Generic; using System.Linq; using System.Reflection; protected List<KeyValuePair<Type, PropertyInfo>> RecrusiveReflectionCompare<T>(T first, T second) where T : class { var differences = new List<KeyValuePair<Type, PropertyInfo>>(); var parentType = first.GetType(); void CompareObject(object obj1, object obj2, PropertyInfo info) { if (!obj1.Equals(obj2)) { differences.Add(new KeyValuePair<Type, PropertyInfo>(parentType, info)); } } foreach (PropertyInfo property in parentType.GetProperties()) { object value1 = property.GetValue(first, null); object value2 = property.GetValue(second, null); if (property.PropertyType == typeof(string)) { if (string.IsNullOrEmpty(value1 as string) != string.IsNullOrEmpty(value2 as string)) { CompareObject(value1, value2, property); } } else if (property.PropertyType.IsPrimitive) { CompareObject(value1, value2, property); } else { if (value1 == null && value2 == null) { continue; } differences.Concat(RecrusiveReflectionCompare(value1, value2)); } } return differences; }

public IEnumerable<PropertyInfo> GetNotEqualsProperties(Employee emp1, Employee emp2)
{
    Type employeeType = typeof (Employee);
    var properies = employeeType.GetProperties();
    foreach (var property in properies)
        if(!property.GetValue(emp1, null).Equals(property.GetValue(emp2, null))) //TODO: check for null
            yield return property;
}

0
投票
public class Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }

    public override bool Equals(object obj)
    {
        if (obj as Address == null)
            return false;
        return ((Address) obj).AddressLine1.Equals(AddressLine1);
    }
}

我尝试了很多在互联网上找到的例子。我自己写的。

0
投票

无需反思。当然,这个示例返回一个带有属性名称的字符串...如果您需要实际的 
PropertyInfo

-1
投票

public static IEnumerable<string> DiffEmployees (Employee one, Employee two) { if(one.FirstName != two.FirstName) yield return "FirstName"; if(one.LastName != two.LastName) yield return "LastName"; if(one.Address.AddressLine1 != two.Address.AddressLine1) yield return "Address.AddressLine1"; // And so on. }

    

© www.soinside.com 2019 - 2024. All rights reserved.