嵌套配置节app.config我没有在app.config中找到任何关于如何访问这种嵌套配置节的例子我以前使用过ConfigurationElementCollection和ConfigurationElement。但我不知道如何做到以上几点?您需要:将my.configuration定义为一个部分组,并将emailNotification定义为该组中的一个部分。将以下内容添加到配置文件:实施配置部分组(my.configuration)。公共类MyConfigurationGroup:ConfigurationSectionGroup{[ConfigurationProperty("emailNotification")]publicEmailNotificationSectionEmailNotification{get{return(EmailNotificationSection)base.Sections["emailNotification"];}}}实现配置部分(emailNotification)。publicclassEmailNotificationSection:ConfigurationSection{[ConfigurationProperty("to")]publicValueElementTo{get{return(ValueElement)base["to"];}}[ConfigurationProperty("from")]publicValueElementFrom{get{return(ValueElement)base["from"];}}[ConfigurationProperty("subject")]publicValueElementSubject{get{return(ValueElement)base["subject"];}}[ConfigurationProperty("smtpHost")]publicValueElementSmtpHost{get{return(ValueElement)base["smtpHost"];}}[ConfigurationProperty("triggers")]publicTriggerElementCollectionTriggers{get{return(TriggerElementCollection)base["triggers"];}}}实际上必须的配置元素和配置元素集合。publicclassValueElement:ConfigurationElement{[ConfigurationProperty("value")]publicstringValue{get{return(string)base["value"];}设置{基数[“值”]=值;}}}publicclassTriggerElement:ConfigurationElement{[ConfigurationProperty("name")]publicstringName{get{return(string)base["name"];}set{base["name"]=value;}}[ConfigurationProperty("varAlias")]publicstringVarAlias{get{return(string)base["varAlias"];}set{base["varAlias"]=value;}}[ConfigurationProperty("lower")]publicintLower{get{return(int)base["lower"];}set{base["lower"]=value;}}[ConfigurationProperty("upper")]publicintUpper{get{return(int)base["upper"];}set{base["upper"]=value;}}}[ConfigurationCollection(typeof(TriggerElement))]publicclassTriggerElementCollection:ConfigurationElementCollection{publicTriggerElementthis[字符串名称]{get{return(TriggerElment)base.BaseGet(名称);}}publicTriggerElementthis[intindex]{get{return(TriggerElement)base.BaseGet(index);}}protectedoverrideConfigurationElementCreateNewElement(){returnnewTriggerElement();}protectedoverrideobjectGetElementKey(ConfigurationElementelement){return((TriggerElement)element).Name;更新配置文件并实现必要的配置位后,您可以按如下方式访问您的部分:多加注意——Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);MyConfigurationGroupmyConfiguration=(MyConfigurationGroup)config.GetSectionGroup("my.configuration");EmailNotificationSection部分=myConfiguration.EmailNotification;本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
