コード例 #1
0
        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();
            }
        }
コード例 #2
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);
                    var convertedValue = Convert.ChangeType(value, property.PropertyType);
                    property.SetValue(result, convertedValue);
                }
                catch { }
            }
            return(result);
        }
コード例 #3
0
 public OptionsProvider(IOptionsFileCreator optionsFileCreator)
 {
     optionsFileCreator.CreateOptionsFileIfNotExists(optionsFilename);
     Options = GetOptions();
 }