[英]C#/WinForms/INotifyPropertyChanged: Elegant way to check for equality when either or both operands might be null
I have a class Bar that implements INotifyPropertyChanged. 我有一个实现INotifyPropertyChanged的类Bar。 When setting the property CurrentFoo, I want to raise PropertyChanged if the value is changing.
设置属性CurrentFoo时,我想在值发生变化时引发PropertyChanged。 Foo implements IEquatable but not the == operator.
Foo实现了IEquatable但不是==运算符。
Currently my code looks like this: 目前我的代码如下所示:
public class Bar : INotifyPropertyChanged
{
// ...
private Foo fooValue;
/// <summary>
/// Gets or sets the active Foo
/// </summary>
/// <value>The active Foo.</value>
public Foo CurrentFoo
{
get
{
return this.fooValue;
}
set
{
// Notify listeners if value is changing.
if (null == this.fooValue && null != value
|| null != this.fooValue && null == value
|| null != this.fooValue && null != value && !this.fooValue.Equals(value))
{
this.fooValue = value;
this.OnPropertyChanged("CurrentFoo");
}
}
}
// ...
}
And it woks, but...ugleh! 它开始了,但是......哎呀! Is there a more elegant/best-practices way of doing this check without restoring to the Null object pattern (inconsistent with the rest of our codebase)?
是否有一种更优雅/最佳实践的方法来执行此检查而无需恢复到Null对象模式(与我们的其余代码库不一致)?
I've considered writing a utility method like IsOneObjectNullButNotBoth(object a, object b)...but again, blah. 我考虑过编写一个像IsOneObjectNullButNotBoth(对象a,对象b)这样的实用工具方法......但是,再次,等等。 Surely I'm missing a handy-dandy class library method, though I already checked those.
当然我错过了一个方便的类库方法,虽然我已经检查过了。
Testing one of the values for null is enough, because the Equals method should return false if the argument is null: 测试null的一个值就足够了,因为如果参数为null,则Equals方法应该返回false:
if ((this.fooValue != value) ||
(this.fooValue != null && !this.fooValue.Equals(value)))
{
this.fooValue = value;
this.OnPropertyChanged("CurrentFoo");
}
Alternatively, you can use the default EqualityComparer for Foo which checks for nulls and calls the Equals method: 或者,您可以使用默认的EqualityComparer for Foo来检查空值并调用Equals方法:
if (!EqualityComparer<Foo>.Default.Equals(this.FooValue, value))
{
this.fooValue = value;
this.OnPropertyChanged("CurrentFoo");
}
It seems like you could use an extension method here: 看起来你可以在这里使用扩展方法:
if(fooValue.IsChanged(value))
{
}
static public class Ext {
static public bool IsChanged(this Foo fooValue, Foo value) {
return null == fooValue && null != value
|| null != this.fooValue
&& null == value
|| null != this.fooValue
&& null != value
&& !this.fooValue.Equals(value));
}
}
This is built into the .net framework and will do just this 这内置于.net框架中,并将执行此操作
if (!System.Collections.Generic.EqualityComparer<Foo>.Default.Equals(value, fooValue))
{
// value changed logic here.
}
Well, may be this way: 好吧,可能就是这样:
Should work. 应该管用。
public Foo CurrentFoo
{
get
{
return this.fooValue;
}
set
{
// Notify listeners if value is changing.
bool bok = false;
if (this.fooValue !=null && !this.fooValue.Equals(value))
bok = true;
else if(this.fooValue ==null)
bok = (this.fooValue == value);
if(bok) {
this.fooValue = value;
this.OnPropertyChanged("CurrentFoo");
}
}
}
This is how I would write it if I didn't want to use EqualityComparer
: 如果我不想使用
EqualityComparer
这就是我写它的方式:
if ((obj1 ?? obj2) != null && (obj1 == null || !obj1.Equals(obj2)))
{
// objects are diffrent
}
I think that's about as elegant as I can make it. 我认为这就像我能做到的一样优雅。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.