Wenn MyApp.PluginsConfiguration ein Configuration ist, dann können Sie eine neue Klasse definieren, die von Configuration erbt und die neue Klasse
prüfen this article für einige vertiefte Informationen über eine Configuration von MyApp.PluginsConfiguration machen diese Arten. Ich auch blogged about geschachtelte Eigenschaften, aber nicht speziell für Sammlungen.
Edit: Hier mit etwas Code. Gegeben ist dieses Bit im web.config:
<configSections>
<section name="plugins"
type="WebApplication1.PluginsConfiguration, WebApplication1"/>
</configSections>
<plugins>
<use assembly="MyApp.Plugin1.dll"/>
<use assembly="MyApp.Plugin2.dll"/>
</plugins>
Hier sind die Klassen um das zu ermöglichen. Beachten Sie, dass möglicherweise NullReferenceExceptions behandelt werden müssen.
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;
}
}
}
}
Und es zugreifen kann, sollten diese Code-Schnipsel helfen:
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();
Grundsätzlich haben wir eine Configuration, die das Plug-ins-Element behandelt. In diesem geben wir eine ConfigurationElementCollection-Eigenschaft an und deklarieren sie als die Standard-Sammlung (Sie könnten theoretisch mehrere verschiedene Sammlungen unter einem Stammknoten haben).
PluginsElementCollection implementiert die ConfigurationElementCollection. ElementName muss der Name des Tags sein, in unserem Fall "use". Außerdem muss GetElementKey außer Kraft gesetzt werden und ein Attribut zurückgeben, das unter den Einträgen eindeutig ist.
PluginsElement implementiert dann ein einmal verwendbares Tag. Wir definieren nur eine Eigenschaft: AssemblyName, die dem assembly-Attribut zugeordnet ist.
Ich behaupte nicht vollständig, dies alles zu verstehen (vor allem Configuration und es verschiedene BaseAdd, BaseGet usw. Eigenschaften nicht wirklich hier erforscht), aber ich kann behaupten, dass dies funktioniert :)
auch an, verwendet keine Attribute. Ich hasse Attribute - zum Glück können alle diese Attribute in den richtigen Code umgewandelt werden. Du könntest das eine oder das andere (oder beide) benutzen.
Auch wenn Sie wollen, schauen Sie sich System.Web.Configuration.CustomErrorsSection und andere mit entweder Reflektor oder der Debugging-Server. Wenn man sich ansieht, wie Microsoft diese (IMHO ziemlich komplizierten) Klassen implementiert, gibt das einen guten Einblick. –
Besser als MSDN-Dokumente! thanx – Aliostad
Definitiv besser als MSDN. Vielen Dank! – Sergejs