3

我花了两天时间追捕一个邪恶的虫子。我通过这个测试案例缩小了问题的范围,展示了实现相同接口的不同集合类的不同行为。具体Contains(null)抛出一个NullArgumentExceptionforImmutableSortedSet但不是 forArray或 mutable SortedSet

using System.Collections.Generic;
using System.Collections.Immutable;
using Xunit;

public class SortedSetSpec
{
    [Fact]
    public void WTFNullCheck()
    {
        var data = new[] {"a", "b", "c", "d"};

        SortedSet<string> ss = new SortedSet<string>(data);
        ImmutableSortedSet<string> iss = ss.ToImmutableSortedSet();

        // This passes
        data.Contains(null).Should().BeFalse();

        // This passes
        ss.Contains(null).Should().BeFalse();

        // This throws an exception
        iss.Contains(null).Should().BeFalse();


    }
}

鉴于所有类 Array / SortedSet 和 ImmutableSortedSet 实现ICollection<T>不应该在Contains(null)调用中具有相同的行为吗?

ImmutableSortedSet当我将数据绑定到ItemsSource列表框的属性时,该错误就显现出来了。

 <ListBox x:Name="ListBox" Margin="2" 
          ItemsSource="{Binding WorkflowTags}" 
          SelectedItem="{Binding SelectedTag}" >
 </ListBox>

问题是在数据绑定代码 ListBox (又名 Selector )深处的某个时刻询问集合Contains(null)?然后如果我绑定了一个ImmutableSortedSet.

那么这是一个带有ImmutableCollections或带有WPF或预期行为的错误,我应该知道比使用更好ImmutableSortedSet吗?

导致问题的代码ImmutableSortedSet可以在corefx github repo中找到

/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public bool Contains(T value)
{
    Requires.NotNullAllowStructs(value, nameof(value));
    return _root.Contains(value, _comparer);
}
4

0 回答 0