NopCommerce学习笔记(一)----IConfigurationSectionHandler 接口的用法
来源:互联网 发布:org.apache.http 报错 编辑:程序博客网 时间:2024/05/16 19:41
IConfigurationSectionHandler:
处理对特定的配置节的访问。
备注
重要事项 IConfigurationSectionHandler 在.NET Framework 2.0 及更高版本已弃用。 但是,因为它在内部使用,而保留。 您可以找到处的一个示例 How to: Create Custom Configuration Sections Using IConfigurationSectionHandler。 如果使用上面的示例,请把它生成带有.NET Framework 版本 1.0 或 1.1。
在.NET Framework 2.0 版和更高版本,你必须改为派生自 ConfigurationSection 类,以实现相关的配置节处理程序。 您可以找到处的一个示例 How to: Create Custom Configuration Sections Using ConfigurationSection。
实例 IConfigurationSectionHandler 类必须是线程安全且无状态。 Create 方法同时必须是可从多个线程调用。
此外,通过生成的配置对象 Create 方法必须是线程安全且不可变。 因为由配置系统缓存的配置的对象,很重要,不能修改的父参数 Create 方法。例如,如果返回值为 Create 是小型进行修改的父级,必须在其父级不是原始的克隆上进行实际修改。
以上来自MSDN。
上代码:
命名空间:ConsoleApp
public class MySection : IConfigurationSectionHandler { public object Create(object parent, object configContext, XmlNode section) { Person person = new Person(); foreach (XmlNode item in section.ChildNodes) { if (item.Name == "name") { person.Name = item.Attributes["value"].Value; } else if(item.Name == "address") { person.Address = item.SelectSingleNode("/MySection/address").InnerText; } else if(item.Name == "age") { person.Age = int.Parse(item.Attributes["value"].Value); } } return person; } } public class Person { public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } }
实现IConfigurationSectionHandler接口
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="MySection" type="ConsoleApp.MySection,ConsoleApp" /> </configSections> <MySection> <name value="John" /> <age value="29" ></age> <address value="123@123" >123@123</address> </MySection> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup></configuration>
config配置文件
var person = ConfigurationManager.GetSection("MySection") as Person;
最终调用
ConfigurationSection类使用心得
转载:
ConfigurationSection类主要是方便我们用于扩展自定义webcongfig中的节点信息。我们可以方便的通过以下方式获取【自定义节点对象】
【你自定义的对象】 config = (【你自定义的对象】)ConfigurationManager.GetSection("【你自定义的节点名称,如果是sectiongroup的话,请使用XPATH方式】");
使用自定义节点,可能会涉及到这几个对象的使用:ConfigurationSection【配置域】、ConfigurationElement【节点】、ConfigurationElementCollection【节点列表】
用法一: 配置如下webconfig自定义信息,注意order和lineItem节点都是允许重复出现的
2<configuration>
3 <configSections>
4 <section name="orders" type="ConsoleTest.OrdersSection, ConsoleTest"/>
5 </configSections>
6 <orders companyID="2001">
7 <order number="100001" amount="222.22">
8 <lineItems warehouseNumber="02">
9 <lineItem number="00-000-001" description="wii"/>
10 </lineItems>
11 </order>
12 <order number="300001" amount="33.33">
13 <lineItems warehouseNumber="99">
14 <lineItem number="00-000-001" description="xbox 360"/>
15 <lineItem number="00-000-003" description="playstation 3"/>
16 </lineItems>
17 </order>
18 </orders>
19</configuration>
下面我们要定义相应的实体对象,该实体对象中会有一个子对象【用来表示节点列表信息】(ConfigurationElementCollection)
{
[ConfigurationProperty("companyID", IsRequired = true)]
public string CompanyID
{
get
{
return (string)base["companyID"];
}
set
{
base["companyID"] = value;
}
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public OrderElementCollection Orders
{
get
{
return (OrderElementCollection)base[""];
}
}
}
接下来我们在看看节点列表对象的定义,其中会包含一个子对象【ConfigurationElementCollection】
{
protected override ConfigurationElement CreateNewElement()
{
return new OrderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((OrderElement)element).Number;
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "order";
}
}
public OrderElement this[int index]
{
get
{
return (OrderElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
}
那么我们再看看节点对象的定义
{
[ConfigurationProperty("number", IsRequired = true)]
public string Number
{
get
{
return (string)base["number"];
}
set
{
base["number"] = value;
}
}
[ConfigurationProperty("amount", IsRequired = true)]
public double Amount
{
get
{
return (double)base["amount"];
}
set
{
base["amount"] = value;
}
}
[ConfigurationProperty("lineItems", IsDefaultCollection = true)]
public LineItemElementCollection LineItems
{
get
{
return (LineItemElementCollection)base["lineItems"];
}
}
}
另外,还有一个子节点列表对象需要定义,【LineItemElementCollection】(ConfigurationElementCollection)
{
[ConfigurationProperty("warehouseNumber", IsRequired = true)]
public string WarehouseNumber
{
get
{
return (string)base["warehouseNumber"];
}
set
{
base["warehouseNumber"] = value;
}
}
protected override ConfigurationElement CreateNewElement()
{
return new LineItemElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ( (LineItemElement)element ).Number;
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "lineItem";
}
}
public LineItemElement this[int index]
{
get
{
return (LineItemElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
}
当然我们还得再定义一个节点对象【LineItemElement
{
[ConfigurationProperty("number", IsKey=true, IsRequired = true)]
public string Number
{
get
{
return (string)base["number"];
}
set
{
base["number"] = value;
}
}
[ConfigurationProperty("description", IsRequired = true)]
public string Description
{
get
{
return (string)base["description"];
}
set
{
base["description"] = value;
}
}
}
】
这样我们就完成了webconfig节点的自定义和对象的实体化, 我们在使用的使用值需要简单的代码就能获取到相应对象的实体信息;如:
OrdersSection config = (OrdersSection)ConfigurationManager.GetSection("orders");
另一中用法:sectionGroup 配置 。如要配置出如下webconfig信息
<sectionGroup name="mygroup">
<section name="mysection"
type="ConfigSection"
allowDefinition="Everywhere"
allowLocation="true"/>
</sectionGroup>
</configSections>
<mygroup>
<mysection user="用户" password="密码">
<element element1="属性1" element2="属性2"></element>
</mysection>
</mygroup>
那么我们看下实体是如何定义的 。
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// ConfigSection 的摘要说明
/// </summary>
public class ConfigSection:ConfigurationSection
{
public ConfigSection()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[ConfigurationProperty("user",DefaultValue="yanghong",IsRequired=true)]
public string User
{
get { return (string)this["user"]; }
set { this["user"] = value; }
}
[ConfigurationProperty("password",DefaultValue="password",IsRequired=true)]
public string PassWord
{
get { return (string)this["password"]; }
set { this["password"] = value; }
}
[ConfigurationProperty("element")]
public elementinfo Element
{
get { return (elementinfo)this["element"]; }
set {this["element"] = value; }
}
}
上面的实体对象包含一个节点对象信息,我们看下这个对象是如何定义的 。
{
public elementinfo() { }
[ConfigurationProperty("element1", DefaultValue = "element1", IsRequired = true)]
public string Element1
{
get { return (string)this["element1"]; }
}
[ConfigurationProperty("element2",DefaultValue="element2",IsRequired=true)]
public string Element2
{
get { return (string)this["element2"]; }
}
}
代码的调用就相当简单了 :
Response.Write("用户名:"+config.User.ToString() + "密码:" + config.PassWord.ToString() + "元素属性:" + config.Element.Element1.ToString() + config.Element.Element2.ToString());
- NopCommerce学习笔记(一)----IConfigurationSectionHandler 接口的用法
- IConfigurationSectionHandler 接口的用法
- NopCommerce学习笔记(一)-----Application_Start
- nopCommerce学习笔记
- .net IConfigurationSectionHandler接口应用
- 也谈IConfigurationSectionHandler接口
- NopCommerce 源码研究(一)
- 实现接口的枚举类 学习笔记(一)
- 菜鸟学习IHttpModule 接口笔记(一)
- 安卓学习笔记(一)Log的用法
- C学习笔记一(scanf用法)
- IConfigurationSectionHandler的线程安全性问题
- Java学习笔记(三)Map接口及其重要实现类的用法
- c#学习笔记一 关于接口的几个疑问
- 代码大全2学习笔记(一):良好的接口
- 接口的学习笔记
- NopCommerce学习资料网址
- Code Complete学习笔记(一)——良好的类接口
- 创作了一幅春天的画作
- [RK3288][Android7.1.2] kernel移植 : rk808+edp
- java 获取本机的IP地址
- 位操作实现加减乘除
- Linux系统下利用apt-get命令源库搭建WordPress
- NopCommerce学习笔记(一)----IConfigurationSectionHandler 接口的用法
- 阻塞队列--详解2
- 最佳完美匹配性质2 uva11383-点上的性质
- Android新特性介绍,ConstraintLayout完全解析 标签: androidio大会android studioConstraint新布局 2017-02-03 09:14 55781人
- Windows 程序设计(1):起步
- java File类 mkdir()与mkdirs()的区别
- python datetime模块
- 刷题——Smallest Difference POJ
- 快速排序