c# - WindowsBase.dll 中发生类型为 'System.InvalidCastException' 的第一次机会异常

标签 c# wpf casting listbox equality

将 ListBox 的 ItemsSource 绑定(bind)到 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; }
}

当我阅读偷看的代码时,它会爆炸。

这样对吗?

最佳答案

确认错误,重现:

[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

关于c# - WindowsBase.dll 中发生类型为 'System.InvalidCastException' 的第一次机会异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585430/

相关文章:

c# - 将 int 转换为 short 会产生意想不到的结果

c++ - 在 C++ 中使用 istringstream 进行字符串到双重转换的意外结果

c# - 是否可以重定向到 MVC 中的非 Controller 类查看

c# - Java <-> Java/C#/Objective-C 之间的通信

c# - FillRectangle 未使用提供的 SolidBrush 的精确颜色进行填充

c# - PreviewMouseMove 触发两次

wpf 数据表 -> 数据网格

c# - OperationContext 中的 header

wpf - ItemsControl ItemsSource 延迟加载

arrays - 如何快速将字典分配给 AnyObject