Geo*_*uer 8 .net configuration config-files
我想有一个xml配置文件,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="plugins" type="MyApp.PluginsConfiguration, MyApp"/>
</configSections>
<plugins>
<use assembly="MyApp.Plugin1.dll" />
<use assembly="MyApp.Plugin2.dll" />
</plugins>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我如何安排我的代码中的PluginsConfigurations,UsePluginCollection和UsePlugin类之间的相互作用?
我在网上找到了这个教程,但它会在插件中引入一个围绕使用集合的元素,我不需要这个.
这是我到目前为止所做的,但不太正确
Mic*_*tum 17
如果MyApp.PluginsConfiguration是ConfigurationSection,那么您可以定义一个继承自ConfigurationElementCollection的新类,并使新类成为MyApp.PluginsConfiguration的ConfigurationProperty.
查看本文以获取有关这些类型的一些深入信息.我还写了关于嵌套属性的博客,但没有专门针对集合.
编辑:这里有一些代码.鉴于web.config中的这一点:
<configSections>
<section name="plugins"
type="WebApplication1.PluginsConfiguration, WebApplication1"/>
</configSections>
<plugins>
<use assembly="MyApp.Plugin1.dll"/>
<use assembly="MyApp.Plugin2.dll"/>
</plugins>
Run Code Online (Sandbox Code Playgroud)
这是实现这一目标的类.请注意,可能需要处理NullReferenceExceptions.
namespace WebApplication1
{
public class PluginsConfiguration : ConfigurationSection
{
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propPlugins;
static PluginsConfiguration()
{
propPlugins = new ConfigurationProperty(null, typeof(PluginsElementCollection),
null,
ConfigurationPropertyOptions.IsDefaultCollection);
properties = new ConfigurationPropertyCollection { propPlugins };
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public PluginsElementCollection Plugins
{
get
{
return this[propPlugins] as PluginsElementCollection;
}
}
}
public class PluginsElementCollection : ConfigurationElementCollection
{
public PluginsElementCollection()
{
properties = new ConfigurationPropertyCollection();
}
private static ConfigurationPropertyCollection properties;
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "use";
}
}
protected override ConfigurationElement CreateNewElement()
{
return new PluginsElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
var elm = element as PluginsElement;
if (elm == null) throw new ArgumentNullException();
return elm.AssemblyName;
}
}
public class PluginsElement : ConfigurationElement
{
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propAssembly;
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public PluginsElement()
{
propAssembly = new ConfigurationProperty("assembly", typeof(string),
null,
ConfigurationPropertyOptions.IsKey);
properties = new ConfigurationPropertyCollection { propAssembly };
}
public PluginsElement(string assemblyName)
: this()
{
AssemblyName = assemblyName;
}
public string AssemblyName
{
get
{
return this[propAssembly] as string;
}
set
{
this[propAssembly] = value;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要访问它,此代码段应该有所帮助:
var cfg = WebConfigurationManager.GetWebApplicationSection("plugins") as PluginsConfiguration;
var sb = new StringBuilder();
foreach(PluginsElement elem in cfg.Plugins)
{
sb.AppendFormat("{0}<br/>", elem.AssemblyName);
}
testLabel.Text = sb.ToString();
Run Code Online (Sandbox Code Playgroud)
基本上我们有一个处理plugins-element的ConfigurationSection.在这里,我们指定一个ConfigurationElementCollection属性并将其声明为默认集合(理论上你可以在一个根节点下有多个不同的集合).
PluginsElementCollection实现ConfigurationElementCollection.ElementName必须是标记的名称,在我们的例子中是"use".此外,需要重写GetElementKey,并且应返回条目中唯一的属性.
然后,PluginsElement实现单个use标记.我们只定义一个属性:AssemblyName,它映射到assembly-attribute.
我没有声称完全理解所有这些(特别是ConfigurationElementCollection和它的各种BaseAdd,BaseGet等属性在这里没有真正探索过),但我可以声称这有效:)
此外,它不使用任何属性.我讨厌属性 - 幸运的是,所有这些属性都可以转换为正确的代码.您可以使用其中一个(或两个).