四个支持开发者创建自定义配置的类
2006-11-06 10:56
211 查看
1. 说明
"key"属性为键,"value"属性为值
DictionarySectionHandler:返回值为Dictionary对象。
NameValueSectionHandler: 返回值为 NameValueCollection 对象。对应于同一个键的多个值用逗号隔开。
XML中属性数据
SingleTagSectionHandler:返回 Hashtable 对象。
忽略
IgnoreSectionHandler: 返回 null引用。
2. 使用范例
<configuration>
<configSections>
<section name="Sample" type="System.Configuration.SingleTagSectionHandler, System"/>
<configSection>
<sample attr1="1" attr2="2">
</configuration>
Hashtable sample = (Hashtable)ConfigurationSetting.GetConfig("sample");
string attr1 = sample ["attr1"].ToString();
string attr2 = sample ["attr2"].ToString();
3. 自定义配置节
web.config
<configuration>
<configSections>
<section name="CostumConfigurationSettings" type="CostumSectionHandler, MyProc" />
</configSections>
<CostumConfigurationSettings type="Class1, MyProc" Attr1="1">
<SubObjs>
<SubObje type="SubClass1, MyProc" Attr1="1"/>
<SubObje type="SubClass1, MyProc" Attr1="2"/>
</SubObjs>
</CostumConfigurationSettings>
</configuration>
CS
public class CostumSectionHandler : IConfigurationSectionHandler
...{
public object Create(object parent, object configContext, System.Xml.XmlNode section)
...{
XPathNavigator nav = section.CreateNavigator();
string typename = (string) nav.Evaluate("string(@type)");
Type t = Type.GetType(typename);
XmlSerializer ser = new XmlSerializer(t);
return ser.Deserialize(new XmlNodeReader(section));
}
}
[Serializable]
public Class1
...{
private _attr1
[XmlAttribute("Attr1")]
public string Attr1
...{
get ...{return this._attr1;}
set ...{this._attr1 = value;}
}
private SubClass1[] _subObjs;
[XmlArray("SubObjs")]
public SubClass1[] SubObjs
...{
get ...{return this._subObjs;}
set ...{this._subObjs = value;}
}
}
[Serializable]
public SubClass1
...{
private _attr1
[XmlAttribute("Attr1")]
public string Attr1
...{
get ...{return this._attr1;}
set ...{this._attr1 = value;}
}
}
"key"属性为键,"value"属性为值
DictionarySectionHandler:返回值为Dictionary对象。
NameValueSectionHandler: 返回值为 NameValueCollection 对象。对应于同一个键的多个值用逗号隔开。
XML中属性数据
SingleTagSectionHandler:返回 Hashtable 对象。
忽略
IgnoreSectionHandler: 返回 null引用。
2. 使用范例
<configuration>
<configSections>
<section name="Sample" type="System.Configuration.SingleTagSectionHandler, System"/>
<configSection>
<sample attr1="1" attr2="2">
</configuration>
Hashtable sample = (Hashtable)ConfigurationSetting.GetConfig("sample");
string attr1 = sample ["attr1"].ToString();
string attr2 = sample ["attr2"].ToString();
3. 自定义配置节
web.config
<configuration>
<configSections>
<section name="CostumConfigurationSettings" type="CostumSectionHandler, MyProc" />
</configSections>
<CostumConfigurationSettings type="Class1, MyProc" Attr1="1">
<SubObjs>
<SubObje type="SubClass1, MyProc" Attr1="1"/>
<SubObje type="SubClass1, MyProc" Attr1="2"/>
</SubObjs>
</CostumConfigurationSettings>
</configuration>
CS
public class CostumSectionHandler : IConfigurationSectionHandler
...{
public object Create(object parent, object configContext, System.Xml.XmlNode section)
...{
XPathNavigator nav = section.CreateNavigator();
string typename = (string) nav.Evaluate("string(@type)");
Type t = Type.GetType(typename);
XmlSerializer ser = new XmlSerializer(t);
return ser.Deserialize(new XmlNodeReader(section));
}
}
[Serializable]
public Class1
...{
private _attr1
[XmlAttribute("Attr1")]
public string Attr1
...{
get ...{return this._attr1;}
set ...{this._attr1 = value;}
}
private SubClass1[] _subObjs;
[XmlArray("SubObjs")]
public SubClass1[] SubObjs
...{
get ...{return this._subObjs;}
set ...{this._subObjs = value;}
}
}
[Serializable]
public SubClass1
...{
private _attr1
[XmlAttribute("Attr1")]
public string Attr1
...{
get ...{return this._attr1;}
set ...{this._attr1 = value;}
}
}
相关文章推荐
- 自定义java.policy配置(如何让你的类禁止读写文件?禁止创建Socket对象?)
- 虚拟机中创建虚拟机报错:已将该虚拟机配置为使用64位,此主机不支持 Intel VT-x。
- iOS 8支持P2P AirPlay功能,便于开发者创建应用程序预览
- SNF开发平台-SNF.CodeGenerator-升级生成BS页面代码-支持视图-数据库配置-快速开发者的利器
- 基于Spring可扩展Schema提供自定义配置支持
- iis中创建、配置一个web站点,使其可支持asp和php
- 未开启自定义菜单 由于开发者通过接口修改了菜单配置,当前菜单配置已失效并停用。你可以前往开发者中心进行停用。
- C#创建自定义配置节点
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 修改Xcode配置使其支持创建编译iphone上的dylib工程
- 基于Spring可扩展Schema提供自定义配置支持
- Maven配置2-使用Maven创建WEB项目支持Tomcat目录部署
- 创建自定义配置结
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持