Вот нестандартная идея:
Является ли учетные записи объявленными как List<Account>
?
Мне интересно, если Accounts
- это свойство, объявленное как-то отличное от List<Account>
- например, IList<Account>
- и у вас есть где-то статический вспомогательный класс с методом расширения Sort
, который не реализован должным образом.Это может попытаться использовать метод List<T>.Sort
, когда переданный параметр является List<T>
, но сделать это без выполнения необходимого приведения к List<T>
, что приведет к гарантированному StackOverflowException
.
Что язначит это.Предположим, что Account
является свойством некоторого класса, который выглядит примерно так:
public class AccountManager
{
public IList<Account> Accounts { get; private set; }
public AccountManager()
{
// here in the constructor, Accounts is SET to a List<Account>;
// however, code that interacts with the Accounts property will
// only know that it's interacting with something that implements
// IList<Account>
Accounts = new List<Account>();
}
}
А затем предположим, что в другом месте у вас есть этот статический класс с Sort
методом расширения:
public static class ListHelper
{
public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
{
// programmer tries to use the built-in sort, if possible
if (list is List<T>)
{
// only problem is, list is here still typed as IList<T>,
// so this line will result in infinite recursion
list.Sort(comparison);
// the CORRECT way to have done this would've been:
// ((List<T>)list).Sort(comparison);
return;
}
else
{
list.CustomSort(comparison);
return;
}
}
private static void CustomSort<T>(this IList<T> list, Comparison<T> comparison)
{
// some custom implementation
}
}
В этом случае код, который вы разместили, выкинет StackOverflowException
.
Исходный ответ:
Возможно Accounts
- это объект пользовательского класса коллекции, чей Sort
метод вызывает себя?
public class AccountCollection : IEnumerable<Account> {
// ...
public void Sort(Comparison<Account> comparison) {
Sort(comparison); // infinite recursion
}
// ...
}
Возможно, свойство AccountId
вызывает себя?
public class Account {
// ...
public string AccountId {
get { return AccountId; } // infinite recursion
}
// ...
}