比较 GetValue 反射方法中的两个值

发布于 2024-10-23 21:01:26 字数 859 浏览 0 评论 0原文

我得到的 value1value2 都为零,因为它们应该相同,但不相等。

我还能如何比较这两个对象的值?

private bool CheckProjectIsUnique(
    TBR.Domain.Project project,
    List<UniqueProjectType> types,
    out UniqueProjectType uniqueCandidate)
{
    uniqueCandidate = CreateUniqueProjectType(project);

    if (types.Count == 0)
        return true;

    foreach (UniqueProjectType type in types)
    {
        bool exists = true;
        foreach (PropertyInfo prop in type.GetType().GetProperties())
        {
            var value1 = prop.GetValue(type, null);
            var value2 = prop.GetValue(uniqueCandidate, null);
             if (value1 != value2)
             {
                 exists = false;
                 break;
             }
        }
        if (exists)
            return true;
    }

    return false;
}

I am getting value1 and value2 which are both zero as not equal when they should be the same.

How else can I compare the values of these 2 objects?

private bool CheckProjectIsUnique(
    TBR.Domain.Project project,
    List<UniqueProjectType> types,
    out UniqueProjectType uniqueCandidate)
{
    uniqueCandidate = CreateUniqueProjectType(project);

    if (types.Count == 0)
        return true;

    foreach (UniqueProjectType type in types)
    {
        bool exists = true;
        foreach (PropertyInfo prop in type.GetType().GetProperties())
        {
            var value1 = prop.GetValue(type, null);
            var value2 = prop.GetValue(uniqueCandidate, null);
             if (value1 != value2)
             {
                 exists = false;
                 break;
             }
        }
        if (exists)
            return true;
    }

    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水溶 2024-10-30 21:01:26

它们是对象,因此您应该使用 value1.Equals(value2) 来检查 value1 是否不 null

编辑: 更好:使用静态 Object.Equals(value1, value2) (归功于@LukeH)

They are objects, so you should use value1.Equals(value2) with checking if value1 is not null

EDIT: Better: use static Object.Equals(value1, value2) (credits to @LukeH)

苍暮颜 2024-10-30 21:01:26

Equals 继承自 System.Object,如果您不提供自己的 Equals 实现,则无法确保正确比较两个对象。

在您的域对象或您想要评估其与另一个对象的相等性的任何对象中重写 System.Object.Equals 和/或实现 IEquatable

了解更多信息,请阅读本文:

Equals is inherited from System.Object and it won't ensure that both objects would be correctly compared if you don't provide your own implementation of Equals.

Override System.Object.Equals and/or implement IEquatable<T> in your domain objects or any object you want to eval its equality with another one.

Learn more reading this article:

年华零落成诗 2024-10-30 21:01:26

if (value1 != value2) 替换为 if (!object.Equals(value1, value2)) 就可以了。

您当前使用的 != 运算符是非虚拟的,并且由于 GetValue 调用的编译时类型是 object 您将始终测试参考(内)相等性。

使用静态 object.Equals(x,y)< /a> 方法首先测试引用相等性,如果对象不是相同的引用,则将遵循虚拟 等于方法。

Swap if (value1 != value2) for if (!object.Equals(value1, value2)) and you should be good to go.

The != operator that you're currently using is non-virtual, and since the compile-time type of the GetValue calls is object you'll always be testing for reference (in)equality.

Using the static object.Equals(x,y) method instead tests first for reference equality, and if the objects aren't the same reference will then defer to the virtual Equals method.

私野 2024-10-30 21:01:26

如果属性的类型是值类型(例如 int),则 GetValue 返回的值将被装箱到一个对象中。然后 == 将比较这些装箱值的引用。如果你想进行正确的比较,你应该使用 Equals 方法:

class Program
{
    class Test
    {
        public int A { get; set; }
    }

    static void Main(string[] args)
    {
        var testA = new Test { A = 1 };
        var testB = new Test { A = 1 };

        var propertyInfo = typeof(Test).GetProperties().Where(p => p.Name == "A").Single();

        var valueA = propertyInfo.GetValue(testA, null);
        var valueB = propertyInfo.GetValue(testB, null);

        var result = valueA == valueB; // False
        var resultEquals = valueA.Equals(valueB); // True

    }
}

If the type of the property is a value type (like int for example), the value returned by GetValue will be boxed into an object. Then == will compare the references of these boxed values. If you want to have a correct comparison, you should use the Equals method:

class Program
{
    class Test
    {
        public int A { get; set; }
    }

    static void Main(string[] args)
    {
        var testA = new Test { A = 1 };
        var testB = new Test { A = 1 };

        var propertyInfo = typeof(Test).GetProperties().Where(p => p.Name == "A").Single();

        var valueA = propertyInfo.GetValue(testA, null);
        var valueB = propertyInfo.GetValue(testB, null);

        var result = valueA == valueB; // False
        var resultEquals = valueA.Equals(valueB); // True

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文