作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在属性值计算中看到非常奇怪的行为。我有一个属性 HasChanged,如果它的任何依赖属性为 true,则该属性为 true。但我得到了一个结果——所有的论点都是假的,结果是真的。我使用的是MVVM Light框架,每个属性都是INotifyPropertyChanged
这是辅助函数
private static bool PropertyWrapper(bool value, [CallerMemberName] string callerName = "")
{
Logger.Debug($"[{callerName}: {value}]");
return value;
}
private static T PropertyWrapper<T>(Expression<Func<T>> property)
{
var compiled = property.Compile();
var result = (T)compiled.DynamicInvoke();
Logger.Debug($"[{GetName(property)}: {result}]");
return result;
}
private static string GetName<T>(Expression<Func<T>> expr)
{
var mexpr = expr.Body as MemberExpression;
if (mexpr == null) return "(null)";
if (mexpr.Member == null) return "((null))";
return mexpr.Member.Name;
}
这是代码
public virtual bool HasChanged => PropertyWrapper(new[] {
PropertyWrapper(() => TitleChanged),
PropertyWrapper(() => EnglishTitleChanged),
PropertyWrapper(() => OriginalTitleChanged),
PropertyWrapper(() => PlotChanged),
PropertyWrapper(() => OutlineChanged),
PropertyWrapper(() => DirectorChanged),
PropertyWrapper(() => YearChanged),
PropertyWrapper(() => MovieRolesChanged),
PropertyWrapper(() => GenresChanged),
PropertyWrapper(() => CountriesChanged)
}.Any(), "HasChanged");
public bool YearChanged => this.state == State.Edit && this.source.MovieDescription.Year != this.clone.MovieDescription.Year;
public bool TitleChanged => HasTitleChanges();
public bool EnglishTitleChanged => HasEnglishTitleChanges();
public bool OriginalTitleChanged => HasOriginalTitleChanges();
public bool PlotChanged => HasDescriptionChanges();
public bool DirectorChanged => HasDirectorChanges();
public bool OutlineChanged => HasOutlineChanges();
public bool MovieRolesChanged => HasMovieRolesChanges();
public bool CountriesChanged => HasCountriesChanges();
public bool GenresChanged => HasGenresChanges();
以及写入日志的内容
[TitleChanged: False]
[EnglishTitleChanged: False]
[OriginalTitleChanged: False]
[PlotChanged: False]
[OutlineChanged: False]
[DirectorChanged: False]
[YearChanged: False]
[MovieRolesChanged: False]
[GenresChanged: False]
[CountriesChanged: False]
[HasChanged: True]
看起来不可能很无聊,但我无法想象怎么会这样。请解释一下这种行为的原因。
最佳答案
Any
如果集合中有任何元素,则不带参数返回。要获得您想要的内容,您必须检查元素的值,如 Any
谓词:
public virtual bool HasChanged => PropertyWrapper(new[] {
//...
}.Any(q => q), "HasChanged");
关于c# - 尽管集合中的所有值均为 false,Linq Any 仍返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42267326/
我是一名优秀的程序员,十分优秀!