Tôi đang cố gắng viết một phần cấu hình tùy chỉnh rất đơn giản cho một ứng dụng .NET4. Mục tiêu của tôi là thế này:Cấu hình tùy chỉnh đơn giản với bộ sưu tập trong .NET4
<configuration>
<configSections>
<section name="myServices" type="My.ConfigSection, My.Assembly" />
</configSections>
<myServices>
<add name="First" />
<add name="Second" />
</myServices>
</configuration>
Tuy nhiên, tôi cứ bị một ConfigurationErrorsException
: 'yếu tố không được nhận 'thêm'' khi tôi gọi ConfigurationManager.GetSection("myServices")
. Tôi đã nhìn chằm chằm vào nó một lúc rồi nhưng chưa tìm ra điều tôi đang làm sai. Dưới đây là mã của tôi. Đó là ba lớp: ConfigSection
, MyServiceSettingsCollection
và MyServiceSettings
.
Đầu tiên là lớp đại diện cho toàn bộ phần cấu hình. Nó có bộ sưu tập mặc định không tên loại MyServiceSettingsCollection
. Thuộc tính IsDefaultCollection
sẽ cho phép tôi 'thêm' trực tiếp vào bộ sưu tập của tôi từ phần tử gốc.
public sealed class ConfigSection : ConfigurationSection
{
private static readonly ConfigurationProperty _propMyServices;
private static readonly ConfigurationPropertyCollection _properties;
public static ConfigSection Instance { get { return _instance; } }
static ConfigSection()
{
_propMyServices = new ConfigurationProperty(
null, typeof(MyServiceSettingsCollection), null,
ConfigurationPropertyOptions.IsDefaultCollection);
_properties = new ConfigurationPropertyCollection { _propMyServices };
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public MyServiceSettingsCollection MyServices
{
get { return (MyServiceSettingsCollection) base[_propMyServices]; }
set { base[_propMyServices] = value; }
}
protected override ConfigurationPropertyCollection Properties
{ get { return _properties; } }
}
Tiếp theo, chính lớp thu thập. Nó thuộc loại AddRemoveClearMap
.
[ConfigurationCollection(typeof(MyServiceSettings),
CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
public sealed class MyServiceSettingsCollection : ConfigurationElementCollection
{
public MyServiceSettings this[int index]
{
get { return (MyServiceSettings) BaseGet(index); }
set
{
if (BaseGet(index) != null) { BaseRemoveAt(index); }
BaseAdd(index, value);
}
}
public new MyServiceSettings this[string key]
{
get { return (MyServiceSettings) BaseGet(key); }
}
protected override ConfigurationElement CreateNewElement()
{
return new MyServiceSettings();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyServiceSettings) element).Key;
}
}
Và cuối cùng là lớp cho các phần tử trong bộ sưu tập. Bây giờ, lớp này có một thuộc tính nhưng sẽ có nhiều hơn sau này (điều này ngăn cản tôi sử dụng NameValueSectionHandler
).
public class MyServiceSettings : ConfigurationElement
{
private static readonly ConfigurationProperty _propName;
private static readonly ConfigurationPropertyCollection properties;
static MyServiceSettings()
{
_propName = new ConfigurationProperty("name", typeof(string), null, null,
new StringValidator(1),
ConfigurationPropertyOptions.IsRequired |
ConfigurationPropertyOptions.IsKey);
properties = new ConfigurationPropertyCollection { _propName };
}
[ConfigurationProperty("name", DefaultValue = "",
Options = ConfigurationPropertyOptions.IsRequired |
ConfigurationPropertyOptions.IsKey)]
public string Name
{
get { return (string) base[_propKey]; }
set { base[_propKey] = value; }
}
protected override ConfigurationPropertyCollection Properties
{ get { return properties; } }
}
nhưng tốt để biết cách hoạt động - sẽ có điều gì đó tương tự để đạt được trong các tuần tới, vì vậy cảm ơn :-) – Yahia
Điều đó thật lạ. Cả hai 'IsDefaultCollection' và' Options' làm việc cho tôi. Nhưng có chuỗi rỗng cho tên tài sản có vẻ là bắt buộc. –
Nhìn vào nguồn tham chiếu cho ConfigurationPropertyAttribute.cs, thuộc tính IsDefaultCollection chỉ là một trình bao bọc trong thuộc tính Tùy chọn cờ - http://referencesource.microsoft.com/#System.Configuration/System/Configuration/ConfigurationPropertyAttribute.cs,62 –