ConfigurationElementCollection如何拥有自定义属性?配置如下...其他条目实现MyCollection时,“default”属性怎么办?假设您有这个.config文件://相应地更新类型和程序集名称然后,使用此代码:我的收藏)这个[“我的收藏”];}}}[ConfigurationCollection(typeof(EntryElement),AddItemName="entry",CollectionType=ConfigurationElementCollectionType.BasicMap)]publicclassMyCollection:ConfigurationElementCollection{protectedoverrideConfigurationElementCreateNewElement(){returnnewElement();}}protectedoverrideobjectGetElementKey(ConfigurationElementelement){if(element==null)thrownewArgumentNullException("element");返回((EntryElement)元素)。名称;}[配置属性y("default",IsRequired=false)]publicstringDefault{get{return(string)base["default"];}}}publicclassEntryElement:ConfigurationElement{[ConfigurationProperty("name",IsRequired=true,IsKey=true)]publicstringName{get{return(string)base["name"];您可以使用“默认”属性读取配置,如下所示:MySectionsection=(MySection)ConfigurationManager.GetSection("mySection");Console.WriteLine(section.MyCollection.Default);这将输出“A”我不知道ConfigurationElementCollection中是否可以有默认值(它没有看到默认值的任何属性)。我认为您必须自己实施。请看下面的例子。publicclassRepository:ConfigurationElement{[ConfigurationProperty("key",IsRequired=true)]publicstringKey{get{return(string)this["key"];}}[ConfigurationProperty("value",IsRequired=true)]publicstringValue{get{return(string)this["value"];}}}publicclassRepositoryCollection:ConfigurationElementCollection{protectedoverrideConfigurationElementCreateNewElement(){returnnewRepository();}protectedoverrideobjectGetElementKey(ConfigurationElementelement){return(elementasRepository).Key;}publicRepositorythis[intindex]{get{returnbase.BaseGet(index)asRepository;}}publicnewRepositorythis[stringkey]{get{returnbase.BaseGet(key)asRepository;}}}publicclassMyConfig:ConfigurationSection{[ConfigurationProperty("currentRepository",IsRequired=true)]privatestringInternalCurrentRepository{get{return(string)this["currentRepository"];}}[ConfigurationProperty("存储ories",IsRequired=true)]privateRepositoryCollectionInternalRepositories{get{returnthis["repositories"]asRepositoryCollection;}}}下面是XML配置:然后,在您的代码中,访问默认项:MyConfigconf=(MyConfig)ConfigurationManager.GetSection("myConfig");stringmyValue=conf.Repositories[conf.CurrentRepository].Value;当然,MyConfig类可以隐藏访问Repositories和CurrentRepository属性的细节你可以有一个名为DefaultRepository的类(的typeRepository)以返回此属性。这可能有点晚,但它可能会帮助其他人。这是可能的,需要进行一些修改。请参见下面的示例。公共类CustomCollection:ConfigurationElementCollection{protectedoverrideConfigurationElementCreateNewElement(){returnnewCustomElement();}protectedoverrideobjectGetElementKey(ConfigurationElementelement){return((CustomElement)element).Name;}publicCustomElementthis[intindex]{get{return(CustomElement)base.BaseGet(index);}set{if(BaseGet(index)!=null)BaseRemoveAt(index);BaseAdd(索引,值);}}//ConfigurationElementthis[string]现在隐藏在子类中publicnewCustomElementthis[stringname]{get{return(CustomElement)BaseGet(name);}}//ConfigurationElementthis[string]现在公开//但是,必须在第二个参数中输入一个值才能访问属性//否则将调用“this[string]”并返回CustomElement而不是publicobjectthis[字符串名称,字符串str=null]{get{returnbase[name];}set{base[name]=value;}}}如果你想对它进行泛化,这应该会有帮助:使用S系统配置;namespaceAbcd{//ConfigurationElementCollection的通用实现。[ConfigurationCollection(typeof(ConfigurationElement))]publicclassConfigurationElementCollection:ConfigurationElementCollection其中T:ConfigurationElement,IConfigurationElement,new(){protectedoverride(Create)ConfigurationElement();}protectedoverrideobjectGetElementKey(ConfigurationElementelement){return((IConfigurationElement)element).GetElementKey();}publicTthis[intindex]{get{return(T)BaseGet(index);}}publicTGetElement(objectkey){return(T)BaseGet(key);}}}这是上面引用的接口:以上是C#学习教程:如何在ConfigurationElementCollection中拥有自定义属性?如果分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
