C# 静态只读字段导致StackOverflowException

C# 静态只读字段导致StackOverflowException,c#,stack-overflow,C#,Stack Overflow,在ASP.NET网站中,我有static readonly XGeoPhone provider=new XGeoPhone()。在XGeoPhoneclass I的内部有静态只读XPhoneInfo[]phoneInfo=new[]{new XPhoneInfo(…),…250000多个对象…}。当我在开发人员机器或real server上运行此程序时,它会在new XGeoPhone()和StackOverflowException时崩溃。我不太明白它试图在堆栈上而不是堆上创建这个巨大的数组

在ASP.NET网站中,我有
static readonly XGeoPhone provider=new XGeoPhone()。在
XGeoPhone
class I的内部有
静态只读XPhoneInfo[]phoneInfo=new[]{new XPhoneInfo(…),…250000多个对象…}
。当我在开发人员机器或real server上运行此程序时,它会在
new XGeoPhone()
StackOverflowException
时崩溃。我不太明白它试图在堆栈上而不是堆上创建这个巨大的数组?发生了什么事

更新:将崩溃的最简单版本:

public partial class XGeoPhone
{
    public XPhoneInfo GetInfo(long phone)
    {
        return
            phoneInfos.
            FirstOrDefault(pi =>
                pi.Start <= phone &&
                phone <= pi.End);
    }

    static readonly XPhoneInfo[] phoneInfos = new[]
    {
        new XPhoneInfo(2000000000, 2099999999, XCountryId.EG, null, null),
        new XPhoneInfo(2120000000, 2129999999, XCountryId.MA, null, null),
        new XPhoneInfo(2130000000, 2139999999, XCountryId.DZ, null, null),
        new XPhoneInfo(2160000000, 2169999999, XCountryId.TN, null, null),
        new XPhoneInfo(2180000000, 2189999999, XCountryId.LY, null, null),
        .........
    }
}

public class XPhoneInfo
{
    public XPhoneInfo(long start, long end, XCountryId? country, string region, string provider)
    {
        this.Start = start;
        this.End = end;
        this.Country = country;
        this.Region = region;
        this.Provider = provider;
    }

    public long Start { get; private set; }
    public long End { get; private set; }
    public XCountryId? Country { get; private set; }
    public string Region { get; private set; }
    public string Provider { get; private set; }
}

class Program
{
    static readonly XGeoPhone g = new XGeoPhone();

    static void Main(string[] args)
    {
        var v = g.GetInfo(79054567890);
    }
}
公共部分类X检波器
{
公共XPhoneInfo GetInfo(长电话)
{
返回
电话信息。
FirstOrDefault(pi=>

pi.Start好的,我找到了解决方法-我可以创建一个线程,并在线程的构造函数中指定堆栈大小,如前所述。它可以工作。但我决定将那个大数组移动到csv文件。

很可能你有一个递归构造
250000
对象,由代码手动添加?代码气味
…250000多个对象….
闻起来很糟糕!也许你可以共享代码(去掉几乎所有的对象),这样我们就可以看到发生了什么。@buffjape我添加了代码。