如何将Properties.Settings.Default的副本保存到变量? [英] How can I save a copy of Properties.Settings.Default to a variable?

查看:87
本文介绍了如何将Properties.Settings.Default的副本保存到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在选项对话框中有一个还原默认值" 按钮,并且想还原仅以这种形式(而不是整个Properties.Settings.Default)受影响的值.

所以我尝试了:

var backup = Properties.Settings.Default;
Properties.Settings.Default.Reload();
overwriteControls();
Properties.Settings.Default = backup;

但是不幸的是,这不起作用,因为备份在Reload()似乎也发生了变化?为什么以及如何正确执行此操作?

解决方案

Settings类使用单例模式,这意味着它们在任何时候都只能是设置的一个实例.因此,复制该实例将始终引用同一实例.

理论上,您可以使用反射来遍历Settings类中的每个属性,并提取如下值:

        var propertyMap = new Dictionary<string, object>();

        // backup properties
        foreach (var propertyInfo in Properties.Settings.Default.GetType().GetProperties())
        {
            if (propertyInfo.CanRead && propertyInfo.CanWrite && propertyInfo.GetCustomAttributes(typeof(UserScopedSettingAttribute), false).Any())
            {
                var name = propertyInfo.Name;
                var value = propertyInfo.GetValue(Properties.Settings.Default, null);
                propertyMap.Add(name, value);
            }
        }

        // restore properties
        foreach (var propertyInfo in Properties.Settings.Default.GetType().GetProperties())
        {
            if (propertyInfo.CanRead && propertyInfo.CanWrite && propertyInfo.GetCustomAttributes(typeof(UserScopedSettingAttribute), false).Any())
            {
                var value = propertyMap[propertyInfo.Name];
                propertyInfo.SetValue(Properties.Settings.Default, value, null);                    
            }
        }

尽管如此,这有点棘手,如果您的设置很复杂,可能需要做一些工作.您可能需要重新考虑您的策略.除了将值恢复为默认值外,您只能在按下确定"按钮后提交值.无论哪种方式,我认为您都需要逐个属性地将每个值复制到某个临时位置.

I have a "Restore Defaults" button in an options dialog and want to restore the values that are affected in this form only and not the whole Properties.Settings.Default

So I tried:

var backup = Properties.Settings.Default;
Properties.Settings.Default.Reload();
overwriteControls();
Properties.Settings.Default = backup;

But this unfortunately doesn't work since backup seems to change at Reload(), too? Why and how would I do this correctly?

解决方案

The Settings class uses a singleton pattern, meaning their can only ever be one instance of the settings at any one time. So making a copy of that instance will always refer to the same instance.

In theory, you could iterate over each of the properties in the Settings class using reflection and extract the values like this:

        var propertyMap = new Dictionary<string, object>();

        // backup properties
        foreach (var propertyInfo in Properties.Settings.Default.GetType().GetProperties())
        {
            if (propertyInfo.CanRead && propertyInfo.CanWrite && propertyInfo.GetCustomAttributes(typeof(UserScopedSettingAttribute), false).Any())
            {
                var name = propertyInfo.Name;
                var value = propertyInfo.GetValue(Properties.Settings.Default, null);
                propertyMap.Add(name, value);
            }
        }

        // restore properties
        foreach (var propertyInfo in Properties.Settings.Default.GetType().GetProperties())
        {
            if (propertyInfo.CanRead && propertyInfo.CanWrite && propertyInfo.GetCustomAttributes(typeof(UserScopedSettingAttribute), false).Any())
            {
                var value = propertyMap[propertyInfo.Name];
                propertyInfo.SetValue(Properties.Settings.Default, value, null);                    
            }
        }

Although, it's a bit icky, and might need some work if your settings are complex. You might want to rethink your strategy. Rather than restoring the values to defaults, you could only commit the values once the OK button is pressed. Either way, I think you are going to need to copy each of the values to some temporary location on a property by property basis.

这篇关于如何将Properties.Settings.Default的副本保存到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆