public void CreateOptionsFileIfNotExists(string optionsFilename) { if (File.Exists(optionsFilename)) { return; } var options = new OptionsDto(); 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(); } }
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); var convertedValue = Convert.ChangeType(value, property.PropertyType); property.SetValue(result, convertedValue); } catch { } } return(result); }
public OptionsProvider(IOptionsFileCreator optionsFileCreator) { optionsFileCreator.CreateOptionsFileIfNotExists(optionsFilename); Options = GetOptions(); }