我在属性值计算中看到了非常奇怪的行为。 我有一个属性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]
看起来不太沉闷,但我无法想象它会怎样。 请解释一下这种行为的原因。