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

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

问题描述

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

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;

现在我该怎么写这两个员工相比较,返回具有不同值的属性列表的方法。因此,在这个例子中,我希望得到的结果是名字和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 .

推荐答案

您不一定需要反射来进行比较。您可以编写一个比较器类,它的员工或地址的两个实例,并比较每个应匹配字段。对于任何不匹配,你可以(或的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.

无论您返回的PropertyInfo 的MemberInfo ,或只是一个字符串取决于什么呼叫者需要做结果。如果你确实需要访问包含分歧领域,的PropertyInfo /的MemberInfo 可能会更好 - 但只是报告的差异的字符串是probaby足够

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.

反映的主要价值是写的通用的对象的比较器,可以采取的两个实例任何类型的对象的,并比较他们的公共字段和属性。这有助于避免一遍又一遍写的repetetive比较code - 但是这似乎并不像你所处的情况下

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天全站免登陆