コード例 #1
0
 private OptionsDto GetOptions()
 {
     var result = new OptionsDto();
     var optionsFileContent = File.ReadAllText(optionsFilename);
     var options = optionsFileContent.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     foreach (var option in options)
     {
         try
         {
             var nameAndValue = option.Split('=');
             var name = nameAndValue.First().Trim();
             var value = nameAndValue.Last().Trim();
             var property = typeof(OptionsDto).GetProperty(name);
             if (property.PropertyType.IsEnum)
             {
                 var convertedValue = Enum.Parse(property.PropertyType, value);
                 property.SetValue(result, convertedValue);
             }
             else
             {
                 var convertedValue = Convert.ChangeType(value, property.PropertyType);
                 property.SetValue(result, convertedValue);
             }
         }
         catch { }
     }
     return result;
 }
コード例 #2
0
 public void WriteOptionsToFile(string optionsFilename, OptionsDto options)
 {
     using (var sw = File.CreateText(optionsFilename))
     {
         var properties = options.GetType().GetProperties();
         foreach (var property in properties)
         {
             sw.WriteLine($"{property.Name} = {property.GetValue(options)}");
         }
         sw.Close();
     }
 }
コード例 #3
0
 public OptionsHandler(IOptionsFileWriter optionsFileWriter)
 {
     this.optionsFileWriter = optionsFileWriter;
     optionsFileWriter.CreateOptionsFileIfNotExists(optionsFilename);
     Options = GetOptions();
 }