设置xml StackOverflowException错误

时间:2016-04-26 08:05:34

标签: c# stack-overflow setting

当我尝试读取设置时,出现错误。我知道我在某处写错了。单独完成的程序,因为我找不到太多信息。

//设置读取代码

  public static class proSave
    {
        public static propertyClass oku
        {
            get
            {
                XmlSerializer serialize = new XmlSerializer(typeof(propertyClass));
                var stream = new StreamReader("settings.xml");
                return (propertyClass)serialize.Deserialize(stream);

            }
        }
    }

设置应用代码

   namespace property
      {
        [Serializable]
        public class propertyClass
        {
            private string _serverName = proSave.oku.serverName;
            [Description("Server Bağlantı Adı"), Category("Server Setting")]
            public string serverName { get { return _serverName; }}

            private string _databaseName = proSave.oku.serverName;
            [Description("Server Bağlantısı Dosya Adı"), Category("Server Setting")]
            public string databaseName { get { return _serverName; } }

            private string _user = proSave.oku.user;
            [Description("Server Bağlantısı kullanıcı adı"), Category("Server Setting")]
            public string user { get { return _user; } }

            private string _password = proSave.oku.password;
            [Description("Server Bağlantısı kullanıcı şifresi"), Category("Server Setting")]
            public string password { get { return _password; } }

            private int _gridHeight = proSave.oku.gridHeight;
            [Description("Grid Hücre Yükseklik Ayarı \nFormlar Yenilendiğinde etkinleştirilecektir."), Category("Grid Ayarları")]
            public int gridHeight { get { return _gridHeight; } }  


        }
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<propertyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <serverName>1</serverName>
  <databaseName>Kadir</databaseName>
  <user>as</user>
  <gridHeight>40</gridHeight>
</propertyClass>

2 个答案:

答案 0 :(得分:2)

我认为你的propertyClass中有一个循环引用,它引起一个循环,最终导致一个StackOverflow异常(在propertyClass中引用proSave.oku,它是propertyClass的一个实例)。在propertyClass中删除此引用可以解决您的问题 - 您的其余代码可以保留原样:

[Serializable]
public class propertyClass
{
    [Description("Server Bağlantı Adı"), Category("Server Setting")]
    public string serverName { get; set; }

    [Description("Server Bağlantısı Dosya Adı"), Category("Server Setting")]
    public string databaseName { get; set; }

    [Description("Server Bağlantısı kullanıcı adı"), Category("Server Setting")]
    public string user { get; set; }

    [Description("Server Bağlantısı kullanıcı şifresi"), Category("Server Setting")]
    public string password { get; set; }

    [Description("Grid Hücre Yükseklik Ayarı \nFormlar Yenilendiğinde etkinleştirilecektir."), Category("Grid Ayarları")]
    public int gridHeight { get; set; }
}

如果你想扩展实现以防止每次都读取xml文件,你可以进一步修改你的proSave类到&#34; cache&#34;设置,例如:

public static class proSave
{
    private static propertyClass settings;

    private const string SettingsFilePath = "C:\\settings.xml";

    public static propertyClass oku
    {
        get
        {
            if (settings == null)
            {
                settings = GetSettings();
                return settings;
            }

            return settings;
        }
    }

    public static void SaveSettings(propertyClass settings)
    {
        XmlSerializer writer = new XmlSerializer(typeof(propertyClass));

        using (FileStream file = File.Create(SettingsFilePath))
        {
            writer.Serialize(file, settings);
        }
    }

    private static propertyClass GetSettings()
    {
        XmlSerializer serialize = new XmlSerializer(typeof(propertyClass));

        using (var stream = new StreamReader(SettingsFilePath))
        {
            return (propertyClass)serialize.Deserialize(stream);
        }
    }
}

如果在SaveSettings()方法中写回设置时文件被锁定,则需要处理IOExceptions。

您还可以查看替代方案,例如使用内置ConfigurationManager或实现自定义ConfigurationSection。希望这会有所帮助。

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<propertyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <serverName>1</serverName>
  <databaseName>Kadir</databaseName>
  <user>as</user>
  <gridHeight>40</gridHeight>
</propertyClass>

System.StackOverflowException&#39;要抛出的异常类型。