如何利用差异比较的GetType的GetValue两个对象的属性值?属性、差异、对象、两个

2023-09-06 09:59:49 作者:乱一世狂傲

我有以下类:

 公共类Person
{
    公共字符串名字{设置;得到; }
    公共字符串名字{设置;得到; }
    公共角色角色{设置;得到; }
}

公共类角色
{
    公共字符串描述{设置;得到; }
    公共双薪{集;得到; }
    公共布尔HasBonus {集;得到; }
}
 

我希望能够自动提取PERSON1和PERSON2,例如如下的属性值diferences:

 公共静态列表<字符串> DiffObjectsProperties(T A,T B)
{
    名单<字符串>差异=新的名单,其中,字符串>();
    的foreach(VAR p在a.GetType()。GetProperties中())
    {
        VAR V1 = p.GetValue(一,NULL);
        VAR V2 = b.GetType()的getProperty(p.Name).GetValue(B,空)。

        / *如果属性类型是一个类,例如会发生什么角色???
         *我们如何提取角色的属性值?
         *需要想出更好的方法比使用.Namespace!=系统
         * /
        如果(!v1.GetType()
            .Namespace
            .Equals(系统,StringComparison.OrdinalIgnoreCase))
            继续;

        //添加值的差异列表
    }

    返回的差异;
}
 

我怎么能提取作用于人的属性值???

解决方案

 公共静态列表<字符串> DiffObjectsProperties(对象A,对象B)
{
    类型类型= a.GetType();
    名单<字符串>差异=新的名单,其中,字符串>();
    的foreach(在type.GetProperties的PropertyInfo P())
    {
        反对安勤= p.GetValue(一,NULL);
        反对bValue = p.GetValue(B,NULL);

        如果(p.PropertyType.IsPrimitive || p.PropertyType == typeof运算(字符串))
        {
            如果(!aValue.Equals(bValue))
                differences.Add(
                    的String.Format({0}:{1} = {2},p.Name,安勤,bValue)
                );
        }
        其他
            differences.AddRange(DiffObjectsProperties(安勤科技,bValue));
    }

    返回的差异;
}
 
狗狗的睡觉位置,反应了不同的态度,你家属于哪一种

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???

解决方案

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