【问题标题】:Debugging List<T>.Count throws ArgumentException调试 List<T>.Count 抛出 ArgumentException
【发布时间】:2012-07-06 14:53:39
【问题描述】:

我有外部课程Item,如果我这样做:

List<Item> items = new List<Item>();

在调试期间将鼠标悬停在其上显示

'items.Count' 抛出了一个类型为'System.ArgumentException' int {System.ArgumentException}的异常

我不能分享这个类的全部代码,但可能是什么原因。我反编译了类,我看到有 GetHashCode 和 Equals 方法被覆盖。这可能是一个原因吗?

编辑

在调试中,行后

List<Item> items = new List<Item>();

使用即时窗口并在那里写 items.Count 我得到:

'items.Count' threw an exception of type 'System.ArgumentException'
base {System.SystemException}: {"Cannot find the method on the object instance."}
Message: "Cannot find the method on the object instance."
ParamName: null

【问题讨论】:

标签: c# .net visual-studio visual-studio-debugging


【解决方案1】:

快速查看 ILSpy 或 Reflector 表明 List&lt;T&gt;.Count 不可能引发该异常。

/// <summary>Gets the number of elements actually contained in the
///   <see cref="T:System.Collections.Generic.List`1" />.</summary>
/// <returns>The number of elements actually contained in the
///   <see cref="T:System.Collections.Generic.List`1" />.</returns>
public int Count
{
    get { return this._size; }
}

鉴于您在调试期间收到此消息,我相信您有导致此异常的第 3 方控件或插件。尝试清理/重建、重新添加第 3 方引用和/或运行 Visual Studio in safe mode

【讨论】:

  • 我的代码只是创建新 List 的代码。我想通过创建空列表来了解导致此异常的原因。确切地说,在 VS2010 中不会抛出异常,而是将鼠标悬停在列表变量上或使用即时窗口时相同。
猜你喜欢
  • 1970-01-01
  • 2012-08-10
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 2021-12-01
  • 2010-10-17
相关资源
最近更新 更多