[HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public class AddinSectionHandler : IConfigurationSectionHandler {
public object Create(object parent, object context, XmlNode section) {
List<AddinConfigItem> colItems;
if (parent == null) {
colItems = new List<AddinConfigItem>();
}
else {
List<AddinConfigItem> colParent = (List<AddinConfigItem>)parent;
colItems = new List<AddinConfigItem>(colParent);
}
AddinConfigItem item;
foreach (XmlNode nodeItem in section.ChildNodes) {
item = new AddinConfigItem();
item.DllFile = nodeItem.Attributes["dllFile"].Value;
item.AddinClass = nodeItem.Attributes["addinClass"].Value;
colItems.Add(item);
}
return colItems;
}
}
public class AddinConfigItem {
private string _dllFile;
public string DllFile {
get { return _dllFile; }
set { _dllFile = value; }
}
private string _addinClass;
public string AddinClass {
get { return _addinClass; }
set { _addinClass = value; }
}
}
public class AddinSectionHandler : IConfigurationSectionHandler {
public object Create(object parent, object context, XmlNode section) {
List<AddinConfigItem> colItems;
if (parent == null) {
colItems = new List<AddinConfigItem>();
}
else {
List<AddinConfigItem> colParent = (List<AddinConfigItem>)parent;
colItems = new List<AddinConfigItem>(colParent);
}
AddinConfigItem item;
foreach (XmlNode nodeItem in section.ChildNodes) {
item = new AddinConfigItem();
item.DllFile = nodeItem.Attributes["dllFile"].Value;
item.AddinClass = nodeItem.Attributes["addinClass"].Value;
colItems.Add(item);
}
return colItems;
}
}
public class AddinConfigItem {
private string _dllFile;
public string DllFile {
get { return _dllFile; }
set { _dllFile = value; }
}
private string _addinClass;
public string AddinClass {
get { return _addinClass; }
set { _addinClass = value; }
}
}
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Assemblys"
type="Dcms.Common.AddinSectionHandler,Dcms.Common"/>
</configSections>
<Assemblys>
<Assembly dllFile="a.dll" addinClass="a" />
<Assembly dllFile="b.dll" addinClass="b" />
</Assemblys>
</configuration>
参考文章:http://www.codeproject.com/aspnet/ConfigSections.asp