WindowsBase.dll中出现'System.InvalidCastException'类型的第一次机会异常

时间:2014-05-10 19:33:48

标签: c# wpf casting listbox equality

我在将ListBox的ItemsSource绑定到ObservableCollection<object>时遇到此异常。

该集合填充了{DependencyPropertyChangedEventArgs,EventEntry}

我偷看了DependencyPropertyChangedEventArgs并找到了:

public struct DependencyPropertyChangedEventArgs
{
    ...
    public override bool Equals(object obj)
    {
        return this.Equals((DependencyPropertyChangedEventArgs)obj); <- huge cast right here?
    }
}

EventEntry代码:

public class EventEntry
{
    public EventEntry(string name)
    {
        Name = name;
    }
    public string Name { get; private set; }
}

当我阅读偷看的代码时,它被设计为爆炸。

这是对的吗?

1 个答案:

答案 0 :(得分:0)

确认错误,repro:

[Test]
public void Repro()
{
    var args = new DependencyPropertyChangedEventArgs(UIElement.IsEnabledProperty, false, true);
    Assert.Throws<InvalidCastException>(() => args.Equals(1));
}

Confirmed bug in the implementation: (below code is from reference source)

public override bool Equals(object obj)
{
    return Equals((DependencyPropertyChangedEventArgs)obj);
}

Filed a bug on Connect