Nếu MyApp.PluginsConfiguration là một ConfigurationSection, sau đó bạn có thể định nghĩa một lớp mới được thừa kế từ ConfigurationElementCollection và làm cho lớp mới một ConfigurationProperty của MyApp.PluginsConfiguration
Kiểm tra this article đối với một số sâu thông tin về những loại đó. Tôi cũng blogged about có thuộc tính lồng nhau, nhưng không dành riêng cho bộ sưu tập.
Chỉnh sửa: Đây với một số mã. Given là bit này trong web.config:
<configSections>
<section name="plugins"
type="WebApplication1.PluginsConfiguration, WebApplication1"/>
</configSections>
<plugins>
<use assembly="MyApp.Plugin1.dll"/>
<use assembly="MyApp.Plugin2.dll"/>
</plugins>
Đây là các lớp để thực hiện điều đó. Lưu ý rằng có thể NullReferenceExceptions cần phải được xử lý.
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;
}
}
}
}
Và để truy cập vào nó, đoạn mã này sẽ giúp:
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();
Về cơ bản chúng tôi có một ConfigurationSection để xử lý các phần tử plugins. Trong đó, chúng tôi chỉ định một thuộc tính ConfigurationElementCollection và khai báo nó là Bộ sưu tập Mặc định (bạn có thể trong lý thuyết có nhiều bộ sưu tập khác nhau dưới một nút gốc).
PluginsElementCollection triển khai ConfigurationElementCollection. ElementName phải là tên của thẻ, trong trường hợp "sử dụng" của chúng tôi. Ngoài ra, GetElementKey cần phải được ghi đè và phải trả về một thuộc tính duy nhất trong số các mục nhập.
PluginsElement sau đó triển khai một thẻ sử dụng duy nhất. Chúng tôi chỉ xác định một thuộc tính: AssemblyName, được ánh xạ tới thuộc tính assembly.
Tôi không khẳng định hoàn toàn hiểu được tất cả điều này (Đặc biệt ConfigurationElementCollection và nó khác nhau BaseAdd, BaseGet, vv thuộc tính không thực sự khám phá ở đây), nhưng tôi có thể khẳng định rằng công trình này :)
Ngoài ra, Nó không sử dụng bất kỳ thuộc tính nào. Tôi ghét thuộc tính - may mắn thay, tất cả các thuộc tính này có thể được chuyển đổi thành mã thích hợp. Bạn có thể sử dụng một hoặc khác (hoặc cả hai).
Ngoài ra nếu bạn muốn, hãy xem System.Web.Configuration.CustomErrorsSection và những người khác bằng cách sử dụng hoặc là Reflector hoặc Debugging Server. Nhìn vào cách Microsoft triển khai các lớp học (IMHO khá phức tạp) này mang lại cái nhìn sâu sắc. –
Tốt hơn tài liệu MSDN! thanx – Aliostad
Chắc chắn tốt hơn MSDN. Cảm ơn rất nhiều! – Sergejs