gpt4 book ai didi

c# - 尽管集合中的所有值均为 false,Linq Any 仍返回 true

转载 作者:行者123 更新时间:2023-12-02 15:45:31 26 4
gpt4 key购买 nike

我在属性值计算中看到非常奇怪的行为。我有一个属性 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com