public void AddConfigSection(IConfigSection configSection) { if (FindConfigSection(configSection) != null) { throw new ArgumentException("Can not add a section that already exists: " + configSection.SectionName); } ConfigSections.Add(configSection); }
public IConfigSection FindOrCreateConfigSection(string name) { var result = FindConfigSection(name); if (result == null) { result = new ConfigSection(name, true); ConfigSections.Add(result); } return result; }
public static void Startup(string startupParameter) { CurrentConfigPath = startupParameter; // If no custom configuration path is specified if (string.IsNullOrWhiteSpace(CurrentConfigPath)) { // Set current configuration directory/path as default CurrentConfigDirectory = DefaultConfigDirectory; CurrentConfigPath = DefaultConfigPath; } else { // Set current configuration directory based on given path CurrentConfigDirectory = Path.GetDirectoryName(CurrentConfigPath); } // Clear config sections ConfigSections.Clear(); // Get all types in the executing assembly (Configuration Tool) Type[] allTypes = Assembly.GetExecutingAssembly().GetTypes(); foreach (Type type in allTypes) { // Filter to parent namespace "Configuration_Tool.Configuration.Sections" if (type.Namespace.StartsWith("Configuration_Tool.Configuration.Sections")) { // Filter to types with child interface IConfig if (type.GetInterfaces().Contains(typeof(IConfig))) { // Create instance IConfig instance = Activator.CreateInstance(type) as IConfig; // Add to list ConfigSections.Add(instance); } } } // Sort in order of integer value of enumerator 'Type' ConfigSections.Sort((x, y) => (x.Type.CompareTo(y.Type))); // Cache default configuration CacheDefaults(); // Load configuration loadConfig(); }
public void ParseData() { string lineIterator; StreamReader Reader = new StreamReader(this.FullPath); Exception exception = new Exception("Неизвестное количество параметров."); while (!Reader.EndOfStream) { lineIterator = Reader.ReadLine(); List <string> paramsBuf; if (ExcludedSynbols.IndexOf(lineIterator) >= 0 || lineIterator.IndexOf('#') > -1) { continue; } if (SectionNames.IndexOf(lineIterator) >= 0) { ConfigSections.Add(new ConfigSection(lineIterator)); continue; } paramsBuf = new List <string>(lineIterator.Split(',')); if (paramsBuf.Count < 2) { continue; } paramsBuf = CleanParams(paramsBuf); var configRows = ConfigSections.Last().ConfigRows; switch (ConfigSections.Last().Name) { case PATH.SectionName: lineIterator = Reader.ReadLine(); List <PATHNode> parsedNodes = new List <PATHNode>(); List <string> nodesParamsBuf = new List <string>(lineIterator.Split(',')); nodesParamsBuf = CleanParams(nodesParamsBuf); switch (paramsBuf.Count) { case 3: while (nodesParamsBuf.Count == 9) { parsedNodes.Add(new PATHNode( Int32.Parse(nodesParamsBuf[0]), Int32.Parse(nodesParamsBuf[1]), Int32.Parse(nodesParamsBuf[2]), double.Parse(nodesParamsBuf[3]), double.Parse(nodesParamsBuf[4]), double.Parse(nodesParamsBuf[5]), double.Parse(nodesParamsBuf[6]), Int32.Parse(nodesParamsBuf[7]), Int32.Parse(nodesParamsBuf[8]) )); if (parsedNodes.Count == 12) { break; } lineIterator = Reader.ReadLine(); nodesParamsBuf = new List <string>(lineIterator.Split(',')); } ConfigSections.Last().ConfigRows.Add(new PATHType1( paramsBuf[0], Int32.Parse(paramsBuf[1]), paramsBuf[2], parsedNodes.ToArray() )); break; case 2: while (nodesParamsBuf.Count == 12) { parsedNodes.Add(new PATHNode( Int32.Parse(nodesParamsBuf[0]), Int32.Parse(nodesParamsBuf[1]), Int32.Parse(nodesParamsBuf[2]), double.Parse(nodesParamsBuf[3]), double.Parse(nodesParamsBuf[4]), double.Parse(nodesParamsBuf[5]), Int32.Parse(nodesParamsBuf[7]), Int32.Parse(nodesParamsBuf[8]), double.Parse(nodesParamsBuf[6]) )); if (parsedNodes.Count == 12) { break; } lineIterator = Reader.ReadLine(); nodesParamsBuf = new List <string>(lineIterator.Split(',')); } ConfigSections.Last().ConfigRows.Add(new PATHType2( paramsBuf[0], Int32.Parse(paramsBuf[1]), parsedNodes.ToArray() )); break; default: throw exception; } break; case INST.SectionName: switch (paramsBuf.Count) { case 11: configRows.Add(new INSTType3().Parse(paramsBuf)); break; case 12: configRows.Add(new INSTType1().Parse(paramsBuf)); break; case 13: configRows.Add(new INSTType2().Parse(paramsBuf)); break; } break; case AUZO.SectionName: switch (paramsBuf.Count) { case 7: configRows.Add(new AUZOType2().Parse(paramsBuf)); break; case 9: configRows.Add(new AUZOType1().Parse(paramsBuf)); break; } break; case CARS.SectionName: switch (paramsBuf.Count) { case 12: configRows.Add(new CARSType1().Parse(paramsBuf)); break; } break; case CULL.SectionName: switch (paramsBuf.Count) { case 11: if (Int32.TryParse(paramsBuf[3], out int _)) { configRows.Add(new CULLType2().Parse(paramsBuf)); } else { configRows.Add(new CULLType1().Parse(paramsBuf)); } break; case 13: configRows.Add(new CULLType3().Parse(paramsBuf)); break; } break; case ENEX.SectionName: switch (paramsBuf.Count) { case 18: configRows.Add(new ENEXType1().Parse(paramsBuf)); break; } break; case GRGE.SectionName: switch (paramsBuf.Count) { case 11: configRows.Add(new GRGEType1().Parse(paramsBuf)); break; } break; case JUMP.SectionName: switch (paramsBuf.Count) { case 16: configRows.Add(new JUMPType1().Parse(paramsBuf)); break; } break; case OCCL.SectionName: switch (paramsBuf.Count) { case 7: configRows.Add(new OCCLType1().Parse(paramsBuf)); break; case 9: configRows.Add(new OCCLType2().Parse(paramsBuf)); break; } break; case PICK.SectionName: switch (paramsBuf.Count) { case 4: configRows.Add(new PICKType1().Parse(paramsBuf)); break; } break; case TCYC.SectionName: switch (paramsBuf.Count) { case 11: configRows.Add(new TCYCType1().Parse(paramsBuf)); break; } break; case ZONE.SectionName: switch (paramsBuf.Count) { case 10: configRows.Add(new ZONEType1().Parse(paramsBuf)); break; } break; } } Reader.Close(); }