您的位置:首页 > 其它

四个支持开发者创建自定义配置的类

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;}


}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: