设置 xml StackOverflowException 错误

setting xml StackOverflowException error

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

//设置读码

  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>
<?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.WhosebugException '要抛出的异常类型。

我认为您的 propertyClass 中有一个循环引用,它导致一个循环,最终导致 Whosebug 异常(引用 proSave.oku,它是 propertyClass 中 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 class 到 "cache" 设置,例如:

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。希望这有帮助。