C# 属性的get函数出现Stackoverflow异常

C# 属性的get函数出现Stackoverflow异常,c#,sql,stack-overflow,C#,Sql,Stack Overflow,我有以下简单的类来管理我的SQL数据库操作 public class DatabaseManager { private string CommandString { set { CommandString = GetCommandString(commandtype); } get { return CommandString; } } public string c

我有以下简单的类来管理我的SQL数据库操作

public class DatabaseManager
    {


        private string CommandString
        {
             set { CommandString = GetCommandString(commandtype); }
             get { return CommandString; }
        }
        public string commandtype
        {
            set;
            get;
        }



        public DatabaseManager(string commandtype)
        {
            commandtype = this.commandtype;
            CommandString = GetCommandString(commandtype);
        }

        public DatabaseManager()
        {

        }       


        public static SqlConnection CreateConnection()
        {
            return new SqlConnection(Properties.Settings.Default.connectionString);
        }




        //returns a datatable if the command requires a dataadapter
        public DataTable ExecuteSelect()
        {
            var x = new DataTable();
            using (var da = new SqlDataAdapter(CommandString, DatabaseManager.CreateConnection()))
                {
                    da.Fill(x);
                }

            return x;
        }




        private string GetCommandString(string commandtype)
        {


            switch (commandtype)
            {
                // select commands
                case ("SELECTMARGINS"): CommandString = "select * from margins"; break;
                case ("SELECTRANKS"): CommandString = "select * from ranks"; break;
                /...and other commands

            return CommandString;
        }

    }

我在get{return CommandString;}上遇到Stackoverflow异常

您无法设置甚至获取CommandString,在这种情况下,您必须创建一个私有变量

private string _commandString;
public string CommandString
{
     set { _commandString = GetCommandString(commandtype); }
     get { return _commandString; }
}
当前代码中发生的情况是,您正在执行以下操作:

CommandString = "x";
哪个叫

CommandString = GetCommandString(type);
CommandString = GetCommandString(type);
哪个叫

CommandString = GetCommandString(type);
CommandString = GetCommandString(type);
等等…所以它一直循环直到溢出。私有变量可以防止您反复设置相同的属性


此外,看起来您从未实际使用传递到set函数的值,这似乎是一个bug,您不能让属性本身返回,因为它会创建一个无限循环

    private string _CommandString;
    public string CommandString
    {
         set { _CommandString = GetCommandString(commandtype); }
         get { return _CommandString; }
    }

您不能让Get函数本身返回,它只会导致它无限尝试检索自身,直到堆栈溢出

创建要获取并设置为的私有变量:

private string _CommandString;
private string CommandString
{
    //Also you probably want to change commandtype to value, since you will be
    //discarding whatever you attempt to set the variable as
    set { _CommandString = GetCommandString(commandtype); } 
    get { return _CommandString; }
}
get函数是您的问题

 get { return CommandString; }
这相当于以下内容

public string GetCommandString() { 
  return GetCommandString();
}
这只会创建无限递归,最终会抛出StackOverflowException。您需要更改get和set以在保留实际值的支持字段上操作,并使用该字段

private string _commandString;
public string CommandString {
  get { return _commandString; }
  set { _commandString = GetCommandString(commandtype); }
}

您正在CommandString的get中的无限循环中运行,因为returncommandstring调用它自己的getter。引入支持字段。私有字符串_commandString。