1

I have added more settings to c# project. i have Settings.settings, Settings1.settings, ... each of those have same value settings of many types.. id - string, point - Point(), size - Size() but different values.

example:

Settings.settings has data:

id = 'first'; string point = 10@10; Point() size = 111; 111; Size()

Settings1.settings has data:

id = 'second'; string point = 20@20; Point() size = 222; 222; Size()

I choose which settings the program will be using at start (by choosing id of setting from listbox).

I then want to get settings value for selected setting with:

Object myPoint = Config.currentPropertiesAt("point");

/Config class/ -> public static Object currentPropertiesAt(string value)

How can i get that value here from correct Settings?

How can i know what object type it will get returned? That is why i use Object, but this can not be OK.

What is a better way?

4

1 に答える 1

1

必要なすべての設定を含む Interface ISettings を作成することをお勧めします。

interface ISettings
{
    string Id { get;  }
    Point Point { get;  }
    Size Size { get;  }

    ...
}

次に、生成された設定クラスに一致する部分クラスを定義します。

internal sealed partial class Settings : ISettings { }
internal sealed partial class Settings1 : ISettings { }
internal sealed partial class Settings2 : ISettings { }
...

生成さSettings1.Designer.csれたクラスは実際のインターフェイスを実装するため、ここに追加するものは何もありません。

これで、コード内でいつでも ISettings を参照して、設定の値を取得できます。

var currentSettings = Config.CurrentSettings; // returns ISettings
var id = currentSettings.Id;

使用する設定 (1、2、...) を選択するコード (これは Config クラスにあると仮定します) では、正しいクラスを返します。

class Config
{
    // Select the correct object using a string, or enum, or int, or ...
    public static ISettings SetCurrentSettings(string selectedSettings)
    {
        switch (selectedSettings)
        {
            default:
            case "Settings":
                CurrentSettings = Properties.Settings.Default;
                break;
            case "Settings1":
                CurrentSettings = Properties.Settings1.Default;
                break;
            case "Settings2":
                CurrentSettings = Properties.Settings2.Default;
                break;
        }
        return CurrentSettings;
    }
    public static ISettings CurrentSettings { get; private set; }
}
于 2013-12-13T15:35:04.500 に答える