迭代器变量在它来自的IEnumerable中不存在?

问题描述

我有一个类似的方法

private Component[] AssociateComponentsWithParametersAndValues(
            IEnumerable<Component> components,IEnumerable<ComponentParameter> parameters,IEnumerable<ComponentParameterValue> values
            )
        {
            var componentsDictionary = new Dictionary<string,Component>();
            var parametersDictionary = new Dictionary<string,ComponentParameter>();
            var valuesDictionary = new Dictionary<string,ComponentParameterValue>();
            foreach (Component c in components)
            {
                bool componentMatch = components.Any(co => co == c);
                bool identifierMatch = components.Any(co => co.Identifier == c.Identifier);
                if (!componentsDictionary.ContainsKey(c.Identifier))
                    componentsDictionary.Add(c.Identifier,c);
            }
            // Do a bunch of stuff to mutate the components
            return components.ToArray();
        }

您会认为componentMatchidentifierMatch每次都正确吗?相反,componentMatch始终为false,identifierMatch始终为true。此外,标识符(几乎有时偶尔会有一些不良数据)总是唯一的,因此,它不可能找到具有相同标识符的另一个组件。

因此,Component类一定有些奇怪。好吧,这就是它的样子

public class Component : ConfigurationObject
    {
        public string Parent { get; set; }
        public string Type { get; set; }
        public string Module { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string TypeName { get; set; }
        public bool? Enabled { get; set; }
        public string DBIdentifier { get; set; }
        public Dictionary<string,ComponentParameterandValues> ParametersAndValues { get; set; }
        public override string Identifier => DBIdentifier;
    }

这是它实现的类

public abstract class ConfigurationObject
    {
        public abstract string Identifier { get; }
    }

为什么会这样?

解决方法

我能看到此中断的唯一方法是,如果IEnumerable<Component> components是惰性计算的可枚举值,则每次都返回新的迭代器对象。这有效:

var list = new List<Component>
{
    new Component { Identifier = "Foo" },new Component { Identifier = "Bar" },new Component { Identifier = "Baz" },};

foreach (Component c in list)
{
    bool componentMatch = list.Any(co => co == c);
    Console.WriteLine($"Component {c.Identifier} match: {componentMatch}");
}

因为==检查引用的相等性(除非Component覆盖它,但是看起来不一样)。但是,如果不是列表,而是每次迭代都有新结果:

IEnumerable<Component> list = GetList();

foreach (Component c in list)
{
    bool componentMatch = list.Any(co => co == c);
    Console.WriteLine($"Component {c.Identifier} match: {componentMatch}");
}

private static IEnumerable<Component> GetList()
{
    yield return new Component { Identifier = "Foo" };
    yield return new Component { Identifier = "Bar" };
    yield return new Component { Identifier = "Baz" };
}

然后它打印false,因为foreach()Any()各自获得了新的新对象集合,因此它们的引用不匹配。

解决方案是枚举一次,并将组件存储一次,在列表中具体化并使用:

var localComponents = components.ToList();

foreach (Component c in localComponents)
{
    // ...
}