public class CofigurationAttribute:Attribute { public string Name { get; set; } }
public static class Settings { const string settingFile = "Signator.config"; private static XDocument configuration; private static void LoadConfigFile() { Logger.Info($"Settings are loading from {settingFile}."); if (configuration == null) configuration = XDocument.Load(settingFile); } private static void LoadSettings() { LoadConfigFile(); foreach (var property in typeof(Settings).GetProperties().Where(q => q.CustomAttributes.Any(p => p.AttributeType.Name == "CofigurationAttribute"))) { Logger.Info($"Setting is reading (${property.Name})... "); ReadConfigFile(property); Logger.Info($"Setting setted ({property.Name} = {property.GetValue(null)})... "); } Logger.Info($"Settings have been readen."); } private static void ReadConfigFile(PropertyInfo property) { var custom = property.CustomAttributes.FirstOrDefault(q => q.AttributeType.Name == "CofigurationAttribute"); var name = custom.NamedArguments.SingleOrDefault(q => q.MemberName == "Name").TypedValue.Value.ToString(); var element = configuration.Root.Element(name); property.SetValue(null, element.Value); } public static void Initialize() { Logger.Info("Settings.Initialize is running"); LoadSettings(); CheckForSettings(); } private static string SetPhysicalPath(string path) { if (string.IsNullOrWhiteSpace(path)) throw new ArgumentNullException(""); else if (path.Length > 1 & path[1] == ':') return path; else return Path.Combine(RootDir, path); } private static Logger m_Logger = null; public static Logger Logger { get { if (m_Logger == null) m_Logger = LogManager.GetLogger("session"); return m_Logger; } } [Cofiguration(Name = "root_directory")] public static string RootDir { get; private set; } private static string m_LicenseFile = string.Empty; [Cofiguration(Name = "license_file")] public static string LicenseFile { get { return m_LicenseFile; } set { m_LicenseFile = SetPhysicalPath(value); } } private static string m_XmlSignatureConfigFile; [Cofiguration(Name = "xml_signature_config_file")] public static string XmlSignatureConfigFile { get { return m_XmlSignatureConfigFile; } set { m_XmlSignatureConfigFile = SetPhysicalPath(value); } } private static string m_SignatureOutDirectory; [Cofiguration(Name = "signature_out_directory")] public static string SignatureOutDirectory { get { return m_SignatureOutDirectory; } set { m_SignatureOutDirectory = SetPhysicalPath(value); } }
}
}