首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何识别丢失的ConfigurationElement?

如何识别丢失的ConfigurationElement?
EN

Stack Overflow用户
提问于 2014-08-22 11:51:53
回答 2查看 950关注 0票数 2

使用System.Configuration,如何识别一个子配置元素是否完全丢失,或者仅仅是空的?

测试程序

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MissingConfigElementTest
{
    class MyConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get { return (string)base["name"]; }
        }
    }

    class MyConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("empty", DefaultValue = null)]
        public MyConfigurationElement Empty
        {
            get { return (MyConfigurationElement)base["empty"]; }
        }

        [ConfigurationProperty("missing", DefaultValue = null)]
        public MyConfigurationElement Missing
        {
            get { return (MyConfigurationElement)base["missing"]; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var configSection = (MyConfigurationSection)ConfigurationManager.GetSection("mySection");

            Console.WriteLine("Empty.Name: " + (configSection.Empty.Name ?? "<NULL>"));
            Console.WriteLine("Missing.Name: " + (configSection.Missing.Name ?? "<NULL>"));

            Console.ReadLine();
        }
    }
}

测试配置

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="mySection" type="MissingConfigElementTest.MyConfigurationSection, MissingConfigElementTest"/>
  </configSections>
  <mySection>
    <empty />
  </mySection>
</configuration>

输出

输出是

代码语言:javascript
复制
Empty.Name: 
Missing.Name:

问题

我找不到一种方法来区分空的,但是现在的配置元素和整个配置元素被忽略的情况。

我想要这样做,因为如果存在一个元素,我希望确保它通过了某些验证逻辑,但也可以完全排除元素。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-22 12:10:15

您可以搜索以下部分:

代码语言:javascript
复制
var sections = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).Sections;
var exists = sections.Cast<ConfigurationSection>()
                .Any(x => x.SectionInformation.Type.StartsWith("MissingConfigElementTest.MyConfigurationSection"));
票数 2
EN

Stack Overflow用户

发布于 2016-03-22 04:27:40

为此,可以使用ElementInformation.IsPresent属性来确定元素是否存在于原始配置中。

代码语言:javascript
复制
var configSection = ( MyConfigurationSection ) ConfigurationManager.GetSection( "mySection" );

Console.WriteLine( "Empty.Name: " + ( configSection.Empty.ElementInformation.IsPresent ? configSection.Empty.Name : "<NULL>" ) );
Console.WriteLine( "Missing.Name: " + ( configSection.Missing.ElementInformation.IsPresent ? configSection.Missing.Name : "<NULL>" ) );

Console.ReadLine( );

这将输出

代码语言:javascript
复制
Empty.Name:
Missing.Name: <NULL>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25446364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档