在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1. 程序调用ConfigurationManager.GetSection(sectionName) 2. Handler 实现IConfigurationSectionHandler 的 Create(object parent, object configContext, XmlNode section) 方法 3. 通过section XmlNode 解析 section 的 attribute & subNode
运行效果: 代码如下: 1. 调用方法 protected void Page_Load(object sender, EventArgs e) { // 获取 ConfigManager 类型实例 ConfigManager config = (ConfigManager)ConfigurationManager.GetSection("traceFact"); ltrName.Text = config.ForumConfig.Name; ltrOfflineTime.Text = config.ForumConfig.OfflineTime.ToString(); ltrPageSize.Text = config.ForumConfig.PageSize.ToString(); ltrReplyCount.Text = config.ForumConfig.ReplyCount.ToString(); ltrRootUrl.Text = config.ForumConfig.RootUrl.ToString(); } 2. Web.Config <section name="traceFact" type="CustomConfig.GeneralConfigurationHandler, CustomConfig"/> <traceFact type="CustomConfig.ConfigManager, CustomConfig"> <forum name="TraceFact.Net Community"> <root url="http://forum.tracefact.net" /> <replyCount>20</replyCount> <pageSize>30</pageSize> <offlineTime>20</offlineTime> </forum> <blog name="JimmyZhang's Space"> <root url="http://blog.tracefact.net" /> <urlMappings> <rewriteRule> <request>~/(\d{4})/Default\.aspx</request> <sendTo>~/BlogDetail.aspx?year=$1</sendTo> </rewriteRule> </urlMappings> </blog> <!-- 将上面的配置也包含进来 --> <mailServer address="mail.tracefact.net" userName="jimmyzhang" password="123456" /> <greetingStrategy type="ClassLib.ChineseGreeting, ClassLib" /> </traceFact> 3. Handler 代码 public class GeneralConfigurationHandler : IConfigurationSectionHandler { // 这里的section结点为 Web.Config中的greetingStrategy结点 public object Create(object parent, object configContext, XmlNode section) { // 获取结点type属性的值 Type t = Type.GetType(section.Attributes["type"].Value); // 直接将section进行传递 object[] parameters = { section }; // 将要创建的类型实例 object obj = null; try { obj = Activator.CreateInstance(t, parameters); // 使用有参数的构造函数 } catch (Exception ex) { return null; } // obj为结点的 type属性中定义的对象,在这里是 ClassLib.ChineseGreeting return obj; } } public class ConfigManager { private XmlNode section; private ForumConfiguration forumConfig; // private BlogConfiguration blogConfig; // private MailServerConfiguration mailServerConfig; // private IGreetingStrategy greetingStrategy; // 以下类似,省略 ... public ForumConfiguration ForumConfig { get { return forumConfig; } } //public BlogConfiguration BlogConfig { // get { return blogConfig; } //} public ConfigManager(XmlNode section) { this.section = section; forumConfig = new ForumConfiguration(section.SelectSingleNode("forum")); // blogConfig = new BlogConfiguration(section.SelectSingleNode("blog")); // mailServerConfig = new MailServerConfiguration(section.SelectSingleNode("mailServer")); // 以下类似,省略 ... } } // 具体的子结点配置 forum 结点 public class ForumConfiguration { private XmlNode forumNode; // 将 forum 结点传递进来 public ForumConfiguration(XmlNode section){ this.forumNode = section; } public string Name{ get{ return forumNode.Attributes["name"].Value; } } public string RootUrl{ get { return forumNode.SelectSingleNode("root").Attributes["url"].Value; } } public int PageSize{ get { return int.Parse(forumNode.SelectSingleNode("pageSize").InnerText); } } public int ReplyCount{ get{ return int.Parse(forumNode.SelectSingleNode("replyCount").InnerText); } } public int OfflineTime{ get { return int.Parse(forumNode.SelectSingleNode("offlineTime").InnerText); } } } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论