Implement custom section with a Configuration Element Collection c#
嗨,伙计们要在项目中实现一个自定义配置部分。但有些事情我不明白,所以不要工作。
我的app.config如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="DepartmentConfigurationSection" type="Statistics.Config.DepartmentSection , Program1"/> </configSections> <s> <Cash> </Cash> <Departments> </Departments> </s> </configuration> |
我创建了一个名为departmentsection.cs的文件,它包含configurationElement、configurationElementCollection和configurationSection。课程如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | public class DepartmentConfig : ConfigurationElement { public DepartmentConfig() { } public DepartmentConfig(int id, string name) { Id = id; Name = name; } [ConfigurationProperty("Id", IsRequired = true, IsKey = true)] public int Id { get { return (int)this["Id"]; } set { this["Id"] = value; } } [ConfigurationProperty("Name", IsRequired = true, IsKey = false)] public string Name { get { return (string)this["Name"]; } set { this["Name"] = value; } } } public class DepartmentCollection : ConfigurationElementCollection { public DepartmentCollection() { Console.WriteLine("ServiceCollection Constructor"); } public DepartmentConfig this[int index] { get { return (DepartmentConfig)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } public void Add(DepartmentConfig depConfig) { BaseAdd(depConfig); } public void Clear() { BaseClear(); } protected override ConfigurationElement CreateNewElement() { return new DepartmentConfig(); } protected override object GetElementKey(ConfigurationElement element) { return ((DepartmentConfig)element).Id; } public void Remove(DepartmentConfig depConfig) { BaseRemove(depConfig.Id); } public void RemoveAt(int index) { BaseRemoveAt(index); } public void Remove(string name) { BaseRemove(name); } } public class DepartmentConfigurationSection : ConfigurationSection { [ConfigurationProperty("Departments", IsDefaultCollection = false)] [ConfigurationCollection(typeof(DepartmentCollection), AddItemName ="add", ClearItemsName ="clear", RemoveItemName ="remove")] public DepartmentCollection Departments { get { return (DepartmentCollection)base["Departments"]; } } } |
我试图从处理程序获取集合,但没有成功。我尝试这样做,但给出了这个错误:"无法初始化系统配置"。
2请帮帮我!谢谢。。
问题出现在你的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="s" type="Statistics.Config.DepartmentConfigurationSection, Program1"/> </configSections> <s> <Cash> </Cash> <Departments> </Departments> </s> </configuration> |
如您所见,
我认为您需要在DepartmentCollection类添加class-level属性:
1 | [DepartmentCollection(typeof(DepartmentConfig ), AddItemName ="add", CollectionType = ConfigurationElementCollectionType.BasicMap)] |