这是一个奇特的想法:
账户是否被声明为List<Account>
?
我在想,如果Accounts
属性被声明为其他类型,例如IList<Account>
,并且您在某个地方有一个静态的帮助类,其中Sort
扩展方法未正确实现。当传入的参数是List<T>
时,它可能会尝试利用List<T>.Sort
方法,但却没有执行必要的转换为List<T>
,从而导致StackOverflowException
。
我的意思是这样的。假设Account
是某个类的属性,该类看起来像这样:
public class AccountManager
{
public IList<Account> Accounts { get; private set; }
public AccountManager()
{
Accounts = new List<Account>();
}
}
假设您在其他地方有一个静态类,其中包含 Sort
扩展方法:
public static class ListHelper
{
public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
{
if (list is List<T>)
{
list.Sort(comparison);
return;
}
else
{
list.CustomSort(comparison);
return;
}
}
private static void CustomSort<T>(this IList<T> list, Comparison<T> comparison)
{
}
}
在这种情况下,您发布的代码将抛出一个StackOverflowException异常。
原始回答:
也许Accounts是自定义集合类的对象,其Sort方法调用了自身?
public class AccountCollection : IEnumerable<Account> {
public void Sort(Comparison<Account> comparison) {
Sort(comparison);
}
}
也许
AccountId
属性会调用自身?
public class Account {
public string AccountId {
get { return AccountId; }
}
}
List<string>
? - BlueRaja - Danny PflughoeftStackOverflowException
吗? - Dan Tao