/// <summary> /// 通过配置节点加载配置信息,没有缓存 /// </summary> /// <param name="node"></param> /// <returns></returns> public static IMaConfig LoadConfig(XmlElement node) { List <string> _filePaths = new List <string>(); _filePaths.Add(m_defaultConfigFile); XmlElement _node = node; if (_node.Attributes["file"] != null) { string _fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + _node.Attributes["file"].Value); if (System.IO.File.Exists(_fileName)) { XmlDocument _configXml = new XmlDocument(); _configXml.Load(_fileName); _node = _configXml.DocumentElement; _filePaths.Add(_fileName); } } if ((_node.Attributes["type"] != null) && (!string.IsNullOrEmpty(_node.Attributes["type"].Value)) ) { //通过反射获得站点配置对象 Type type = GetType(_node.Attributes["type"].Value); if (type != null) { IMaConfig _obj = Activator.CreateInstance(type) as IMaConfig; if (_obj != null) { _obj.Load(_node); } return(_obj); } } return(null); }
/// <summary> /// 读取配置文件并缓存 /// </summary> /// <param name="node"></param> /// <returns></returns> static IMaConfig ConfigAndCache(XmlElement node) { List <string> _filePaths = new List <string>(); _filePaths.Add(m_defaultConfigFile); #region 读取首个配置文件并得到子配置文件 XmlElement _node = node; if (_node.Attributes["file"] != null) { string _fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + _node.Attributes["file"].Value); if (System.IO.File.Exists(_fileName)) { XmlDocument _configXml = new XmlDocument(); _configXml.Load(_fileName); _node = _configXml.DocumentElement; _filePaths.Add(_fileName); } } #endregion #region 处理子节点外部文件 using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(node.OuterXml))) { while (reader.Read()) { if (reader.GetAttribute("file") != null) { string _fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + reader.GetAttribute("file")); if (System.IO.File.Exists(_fileName)) { _filePaths.Add(_fileName); } } } } #endregion #region 获取站点对应的配置文件 if ((_node.Attributes["type"] != null) && (!string.IsNullOrEmpty(_node.Attributes["type"].Value))) { //通过反射获得站点配置对象 Type type = GetType(_node.Attributes["type"].Value); if (type != null) { IMaConfig _obj = Activator.CreateInstance(type) as IMaConfig; if (_obj != null) { _obj.Load(_node); //缓存配置信息 CacheItemPolicy _cachePolicy = new CacheItemPolicy(); _cachePolicy.ChangeMonitors.Add(new HostFileChangeMonitor(_filePaths)); m_configCache.Set(node.Name, _obj, _cachePolicy); } return(_obj); } } #endregion return(null); }