巨大类上的StackOverflowException

本文关键字:StackOverflowException 巨大 | 更新日期: 2023-09-27 18:02:07

所以,我开始做这个大项目。这是一个巨大的类,有数百个变量和方法,还有很多分部类。

interface IBusinessReturn
{
    string variableOne { get; set; }
    string variableTwo { get; set; }
    string variableHundred { get; set; }
    //a lot more...
 }
 public partial class BusinessTransaction : IBusinessReturn
{
    private string _variableOne;
    public string variableOne
    {
        get { return variableOne; }
        set { _variableOne= value; }
    }
    private string _variableTwo;
    public string variableTwo
    {
        get { return variableTwo; }
        set { _variableTwo = value; }
    }
    private string _variableHundred;
    public string variableHundred
    {
        get { return variableHundred; }
        set { _variableHundred = value; }
    }
    // And so it goes on till hundreds...
}

还有很多像这样的偏导数:

public partial class BusinessTransaction: IBusinessTransaction238
{
   //Lots of methods
}

问题是:除了我声明的一些新变量之外,它都在工作。(上面例子中的var1和var2)。当我试图为这些变量设置任何值时,我得到了一个StackOverflowException。我100%确定它们的声明和其他的一样。

这就是我打电话的方式:

 BusinessTransaction v763 = new BusinessTransaction();
 v763.variableHundred = "Hi"; //working
 v763.variableOne = "Hello"; //StackOverflow HERE.

我不知道为什么会发生这种情况,我只希望你能告诉我这是否与这个类中大量的方法和变量有关。

巨大类上的StackOverflowException

看看你的getter——没有下划线。你正在引起一个无限循环。

public string variableOne
{
    get { return variableOne; }
    set { _variableOne= value; }
}

它应该返回私有成员,而不是自身。

应该是

public string variableOne
{
    get { return _variableOne; // error was here
    }
    set { _variableOne= value; }
}
相关文章:
  • 没有找到相关文章