ImmutableArray<>行为不同于阵列<>嵌套选择索引 [英] ImmutableArray<> behaves differently than Array<> for nested Select with index

查看:220
本文介绍了ImmutableArray<>行为不同于阵列<>嵌套选择索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到什么似乎是一个非常奇怪的错误 ImmutableArray<> (与Bcl不可改变的集合v1.0.12.0,运行.NET 4.5):

I am encountering what seems to be a very weird bug in ImmutableArray<> (with BCL Immutable collections v1.0.12.0, runtime .NET 4.5):

我有以下两个相同的结构相同的命名空间下的同一个源文件恰好:

I have the following two identical structs exactly in the same source file under the same namespace:

public struct WeightedComponent {
    public readonly IComponent Component;
    public readonly decimal Weight;

    public WeightedComponent(IComponent component, decimal weight) {
        this.Component = component;
        this.Weight = weight;
    }
}

public struct WeightedComponent2 {
    public readonly IComponent Component;
    public readonly decimal Weight;

    public WeightedComponent2(IComponent component, decimal weight) {
        this.Component = component;
        this.Weight = weight;
    }
}



下面将抛出一个异常:

The following will throw an exception:

var elements1 = new[] { 1, 2, 3 }.Select(wc => new WeightedComponent(null, 0)).ToImmutableArray();
var foo = elements1.Select((e1, i1) => elements1.Select((e2, i2) => 0).ToArray()).ToArray();
if (foo.Length != 3) throw new Exception("Error: " + foo.Length); //Error: 1

var elements2 = new[] { 1, 2, 3 }.Select(wc => new WeightedComponent2(null, 0)).ToImmutableArray();
var foo2 = elements2.Select((e1, i1) => elements2.Select((e2, i2) => 0).ToArray()).ToArray();
if (foo2.Length != 3) throw new Exception("Error: " + foo.Length);

如果一期项目elements1到 ToArray的()在第一行,而不是 ToImmutableArray(),一切工作正常。

If I project elements1 into ToArray() on the first line instead of ToImmutableArray(), everything works fine.

这两个结构之间的唯一区别是该 WeightedComponent 广泛的代码之前使用,而 WeightedComponent2 之前从未使用过(这就是为什么它可能不是显而易见重现bug)。

The only difference between the two structs is that WeightedComponent is used extensively by the code before, while WeightedComponent2 is never used before (this is why it may not be obvious to reproduce the bug).

迭代的 elements1 在同一个表达式两次似乎有关问题,因为如果我删除它选择工作正常,但与 elements2 没有这样的问题。
这真的似乎与的方式后面的代码 ImmutableArray<> 正在考虑两种结构(?也许有一个缓存机制)

Iterating on elements1 twice in the same expression seem to be related to the issue, as if I remove it the Select works fine, but there is no such problem with elements2. It really seems to be related to the way the code behind ImmutableArray<> is considering both structs (maybe there is a caching mechanism?)

你有什么想法可能是什么原因?

Do you have any idea what could be causing this?

推荐答案

这是由于在 ImmutableArray℃的错误; T> 枚举,引发了第一次枚举集合的空实例。该包的NuGet的未来版本将有问题的解决。在此期间,我强烈建议你避免使用 ImmutableArray<的; T>

This is due to a bug in the ImmutableArray<T> enumerator, triggered the first time you enumerate an empty instance of the collection. A future version of the NuGet package will have the issue fixed. In the meantime, I strongly suggest you avoid use of ImmutableArray<T>.

这篇关于ImmutableArray&LT;&GT;行为不同于阵列&LT;&GT;嵌套选择索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆