使用反射比较对象属性 [英] Comparing Object properties using reflection

查看:13
本文介绍了使用反射比较对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类 Address 和 Employee 如下:

I have two classes Address and Employee as follows:

 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; }
    }

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

I have two employee instances as follows:

    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 .

Now how can I write a method which compares these two employees and returns the list of properties which have different values. So in this example I would like the result to be FirstName and Address.AddressLine1 .

推荐答案

您不一定需要反射来执行比较.您可以编写一个比较器类,它采用 Employee 或 Address 的两个实例, 并比较每个应该匹配的字段.对于任何不匹配的,您可以向某个列表添加一个字符串(或 PropertyInfo)元素以返回给调用者.

You don't necessarily need reflection to perform the comparison. You can write a comparer class that takes two instances of Employee or Address, and compares each field that should match. For any that don't match, you can add a string (or PropertyInfo) element to some list to return to the caller.

返回一个 PropertyInfoMemberInfo 还是一个字符串取决于调用者需要对结果做什么.如果您确实需要访问包含差异的字段,PropertyInfo/MemberInfo 可能会更好 - 但只需报告一个字符串就足够了.

Whether you return a PropertyInfo, MemberInfo, or just a string depends on what the caller needs to do with the result. If you actually need to visit the fields that contain differences, the PropertyInfo/MemberInfo may be better - but to just report the differences a string is probaby sufficient.

反射的主要价值是编写一个通用对象比较器,它可以接受任何类型对象的两个实例,并比较它们的公共字段和属性.这有助于避免一遍又一遍地编写重复的比较代码 - 但您的情况似乎并非如此.

The main value of reflection would be to write a general purpose object comparer that could take two instances of any kind of object and compare their public fields and properties. This helps avoid writing repetetive comparison code over and over - but that doesn't seem like the case you're in.

这篇关于使用反射比较对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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