Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Options
- {
- bool StartWithWindowsType;
- string Name;
- public string Value
- {
- ...
- }
- string DefaultValue;
- protected Options(string name, string def_value, bool start_with_windows=false)
- {
- Name = name;
- DefaultValue = def_value;
- StartWithWindowsType = start_with_windows;
- }
- public static Options StartWithWindows;
- // вызывается извне, инициализируем все опции
- public static void Init()
- {
- StartWithWindows = new Options("StartWithWindows", "1", true);
- }
- // всем полям класса Options задать значение value = значению по умолчанию
- public static void AllToDefault()
- {
- FieldInfo[] fields = typeof(Options).GetFields();
- foreach (System.Reflection.FieldInfo opt in fields)
- ???
- }
- T Value
- {
- get
- {
- var res = Reg.Read(Name);
- if (string.IsNullOrEmpty(res))
- return DefaultValue;
- else
- {
- if (Value.GetType() == typeof(int))
- return Convert.ToInt32(res);
- if (Value.GetType() == typeof(bool))
- return Convert.ToBoolean(res);
- }
- }
- set
- { ...
- void Main()
- {
- var option1 = new Option<string>("City", "Moscow");
- var option2 = new Option<bool>("StartWithWindows", false);
- var option3 = new Option<int>("ThreadsCount", 10);
- option1.Set("Vologda");
- option2.Set(true);
- option3.Set(15);
- // and so on
- option1.Get().Dump();
- option2.Get().Dump();
- option3.Get().Dump();
- }
- // Define other methods and classes here
- public class Option<T>
- {
- public Option(string name, T defaultValue)
- {
- this.Name = name;
- this.DefaultValue = defaultValue;
- this.Value = defaultValue;
- }
- public void Set(T value)
- {
- this.Value = value;
- }
- public string GetName()
- {
- return this.Name;
- }
- public T Get()
- {
- return this.Value;
- }
- public T GetDefaultValue()
- {
- return this.DefaultValue;
- }
- private string Name { get; set; }
- private T DefaultValue { get; set; }
- private T Value { get; set; }
- }
- public class Options
- {
- public Option<string> BirthCity { get; set; }
- public Option<string> CurrentCity { get; set; }
- public Option<bool> StartWithWindows { get; set; }
- public Option<int> ThreadsCount { get; set; }
- }
- // цикл по типу и всем базовым типам
- for (Type type = GetType(); type != null; type = type.BaseType)
- {
- foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly |
- BindingFlags.Public | BindingFlags.NonPublic))
- {
- // ...
- }
- }
- Type fieldType = field.FieldType;
- object defaultValue = fieldType.IsValueType ? Activator.CreateInstance(fieldType) : null;
- for (Type type = GetType(); type != null; type = type.BaseType)
- {
- foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly |
- BindingFlags.Public | BindingFlags.NonPublic))
- {
- Type fieldType = field.FieldType;
- object defaultValue = fieldType.IsValueType ? Activator.CreateInstance(fieldType) :
- null;
- field.SetValue(this, defaultValue);
- }
- }
- field.SetValue(this, defaultValue);
- if (field.Name == "defaultValue" && type == typeof(Options))
- continue;
- field.SetValue(this, defaultValue);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement