比较 GetValue 反射方法中的两个值
我得到的 value1
和 value2
都为零,因为它们应该相同,但不相等。
我还能如何比较这两个对象的值?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它们是对象,因此您应该使用
value1.Equals(value2)
来检查value1
是否不null
编辑: 更好:使用静态
Object.Equals(value1, value2)
(归功于@LukeH)They are objects, so you should use
value1.Equals(value2)
with checking ifvalue1
is notnull
EDIT: Better: use static
Object.Equals(value1, value2)
(credits to @LukeH)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:
将
if (value1 != value2)
替换为if (!object.Equals(value1, value2))
就可以了。您当前使用的
!=
运算符是非虚拟的,并且由于GetValue
调用的编译时类型是object
您将始终测试参考(内)相等性。使用静态
object.Equals(x,y)
< /a> 方法首先测试引用相等性,如果对象不是相同的引用,则将遵循虚拟等于
方法。Swap
if (value1 != value2)
forif (!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 theGetValue
calls isobject
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 virtualEquals
method.如果属性的类型是值类型(例如 int),则 GetValue 返回的值将被装箱到一个对象中。然后
==
将比较这些装箱值的引用。如果你想进行正确的比较,你应该使用 Equals 方法: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: