如何使用GetType GetValue来区分两个对象的属性值?

时间:2022-10-16 20:12:37

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:

我希望能够自动提取Person1和Person2之间的属性值差异,例如如下:

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 个解决方案

#1


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

#2


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.

如果属性不是值类型,为什么不直接在它们上调用DiffObjectProperties并将结果追加到当前列表?据推测,你需要迭代它们并以点符号前缀属性的名称,以便你可以看到不同的东西 - 或者它可能足以知道如果列表非空,则当前属性不同。

#3


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.

是String FirstName或Role Role。我一直试图找出如何判断v1是否是一个字符串,如FirstName或类Role。因此,我不知道何时以递归方式将对象属性(Role)传递回DiffObjectsProperties以迭代其属性值。

#1


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

#2


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.

如果属性不是值类型,为什么不直接在它们上调用DiffObjectProperties并将结果追加到当前列表?据推测,你需要迭代它们并以点符号前缀属性的名称,以便你可以看到不同的东西 - 或者它可能足以知道如果列表非空,则当前属性不同。

#3


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.

是String FirstName或Role Role。我一直试图找出如何判断v1是否是一个字符串,如FirstName或类Role。因此,我不知道何时以递归方式将对象属性(Role)传递回DiffObjectsProperties以迭代其属性值。