Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. class Options
  2. {
  3. bool StartWithWindowsType;
  4. string Name;
  5. public string Value
  6. {
  7. ...
  8. }
  9.  
  10. string DefaultValue;
  11.  
  12. protected Options(string name, string def_value, bool start_with_windows=false)
  13. {
  14. Name = name;
  15. DefaultValue = def_value;
  16. StartWithWindowsType = start_with_windows;
  17. }
  18.  
  19. public static Options StartWithWindows;
  20.  
  21. // вызывается извне, инициализируем все опции
  22. public static void Init()
  23. {
  24. StartWithWindows = new Options("StartWithWindows", "1", true);
  25. }
  26.  
  27. // всем полям класса Options задать значение value = значению по умолчанию
  28. public static void AllToDefault()
  29. {
  30. FieldInfo[] fields = typeof(Options).GetFields();
  31.  
  32. foreach (System.Reflection.FieldInfo opt in fields)
  33. ???
  34. }
  35.  
  36. T Value
  37. {
  38. get
  39. {
  40. var res = Reg.Read(Name);
  41. if (string.IsNullOrEmpty(res))
  42. return DefaultValue;
  43. else
  44. {
  45. if (Value.GetType() == typeof(int))
  46. return Convert.ToInt32(res);
  47. if (Value.GetType() == typeof(bool))
  48. return Convert.ToBoolean(res);
  49. }
  50. }
  51. set
  52. { ...
  53.  
  54. void Main()
  55. {
  56. var option1 = new Option<string>("City", "Moscow");
  57. var option2 = new Option<bool>("StartWithWindows", false);
  58. var option3 = new Option<int>("ThreadsCount", 10);
  59.  
  60. option1.Set("Vologda");
  61. option2.Set(true);
  62. option3.Set(15);
  63.  
  64. // and so on
  65. option1.Get().Dump();
  66. option2.Get().Dump();
  67. option3.Get().Dump();
  68. }
  69.  
  70. // Define other methods and classes here
  71.  
  72. public class Option<T>
  73. {
  74. public Option(string name, T defaultValue)
  75. {
  76. this.Name = name;
  77. this.DefaultValue = defaultValue;
  78. this.Value = defaultValue;
  79. }
  80.  
  81. public void Set(T value)
  82. {
  83. this.Value = value;
  84. }
  85.  
  86. public string GetName()
  87. {
  88. return this.Name;
  89. }
  90.  
  91. public T Get()
  92. {
  93. return this.Value;
  94. }
  95.  
  96. public T GetDefaultValue()
  97. {
  98. return this.DefaultValue;
  99. }
  100.  
  101. private string Name { get; set; }
  102.  
  103. private T DefaultValue { get; set; }
  104.  
  105. private T Value { get; set; }
  106. }
  107.  
  108. public class Options
  109. {
  110. public Option<string> BirthCity { get; set; }
  111.  
  112. public Option<string> CurrentCity { get; set; }
  113.  
  114. public Option<bool> StartWithWindows { get; set; }
  115.  
  116. public Option<int> ThreadsCount { get; set; }
  117. }
  118.  
  119. // цикл по типу и всем базовым типам
  120. for (Type type = GetType(); type != null; type = type.BaseType)
  121. {
  122. foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly |
  123. BindingFlags.Public | BindingFlags.NonPublic))
  124. {
  125. // ...
  126. }
  127. }
  128.  
  129. Type fieldType = field.FieldType;
  130. object defaultValue = fieldType.IsValueType ? Activator.CreateInstance(fieldType) : null;
  131.  
  132. for (Type type = GetType(); type != null; type = type.BaseType)
  133. {
  134. foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly |
  135. BindingFlags.Public | BindingFlags.NonPublic))
  136. {
  137. Type fieldType = field.FieldType;
  138. object defaultValue = fieldType.IsValueType ? Activator.CreateInstance(fieldType) :
  139. null;
  140. field.SetValue(this, defaultValue);
  141. }
  142. }
  143.  
  144. field.SetValue(this, defaultValue);
  145.  
  146. if (field.Name == "defaultValue" && type == typeof(Options))
  147. continue;
  148. field.SetValue(this, defaultValue);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement