转载

https://www.cnblogs.com/lxshwyan/p/10828305.html

如果使用了configSection节点,则configSection必须位于根节点的第0个。App.config中代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CustomerSingleConfig" type="ConfiguraDemo.CustomerSingleConfig,ConfiguraDemo"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<CustomerSingleConfig PlatChName="监控平台系统" PlatEnName="Monitoring platform system"></CustomerSingleConfig>
</configuration>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConfiguraDemo
{
public class CustomerSingleConfig : ConfigurationSection
{
/// <summary>
/// 获取配置信息
/// </summary>
/// <returns></returns>
public static CustomerSingleConfig GetConfig()
{
return GetConfig("CustomerSingleConfig");
}
private static CustomerSingleConfig GetConfig(string sectionName)
{
try
{
CustomerSingleConfig section = (CustomerSingleConfig)ConfigurationManager.GetSection(sectionName);
if (section == null)
throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
return section;
}
catch (Exception ex)
{
throw new ConfigurationErrorsException("Section " + ex.Message);
}
} /// <summary>
/// 平台中文名称
/// </summary>
[ConfigurationProperty("PlatChName", DefaultValue = "", IsRequired = true, IsKey = false)]
public string PlatChineseName
{
get { return (string)this["PlatChName"]; }
set { this["PlatChName"] = value; }
}
/// <summary>
/// 平台英文名称
/// </summary>
[ConfigurationProperty("PlatEnName", DefaultValue = "", IsRequired = true, IsKey = false)]
public string PlatEnglishName
{
get { return (string)this["PlatEnName"]; }
set { this["PlatEnName"] = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConfiguraDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---------------------单级配置节点测试-----------------");
Console.WriteLine("PlatChName:" + CustomerSingleConfig.GetConfig().PlatChineseName);
Console.WriteLine("PlatEnName:" + CustomerSingleConfig.GetConfig().PlatEnglishName);
Console.ReadKey();
}
}
}

输出结果

---------------------单级配置节点测试-----------------
PlatChName:监控平台系统
PlatEnName:Monitoring platform system

继承ConfigurationSection需要添加System.Configuration引用

关于C#Section配置未初始化的问题的更多相关文章

  1. C#异常断电后重新启动项目出现配置未初始化错误

    转到如截图中所示路径,将其下的数据删掉,就可以启动了.

  2. C语言全局未初始化数据段分析

    前言: 在分析C语言全局未初始化变量时,发现在目标文件中全局未初始化变量并不是直接放在bss段中. 再后来发现在两个.c文件中定义同名的全局变量,链接时居然没有发生符号重定义错误.才知道C语言弱定义的 ...

  3. 让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean

    让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean 问题描述 实现思路 思路一 [不符合要求] 思路二[满足要求] 思路三[未试验] 问题描述 目前我工作环境下,后端主要的框架 ...

  4. 三叔学FPGA系列之二:Cyclone V中的POR、配置、初始化,以及复位

    对于FPGA内部的复位,之前一直比较迷,这两天仔细研究官方数据手册,解开了心中的诸多疑惑,感觉自己又进步了呢..... 原创不易,转载请转原文,注明出处,谢谢.   一.关于POR(Power-On ...

  5. springboot hikari 连接池 在启动时未初始化数据库连接问题

    在启动Springboot 项目时 2019-11-18 21:32:38.223 INFO 1080 --- [on(4)-127.0.0.1] o.s.web.servlet.Dispatcher ...

  6. mybatis源码分析--如何加载配置及初始化

    简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...

  7. C++中未初始化的bool值的问题

    原创文件,欢迎阅读,禁止转载. 问题描述 你见过一个这样的bool值吗,判断 var 和 !var 都是成立的,今天被我遇到了,是在一个坑里遇到的.今天调试了一个程序,发送一个网络消息,结果总是得不到 ...

  8. c语言中较常见的由内存分配引起的错误_内存越界_内存未初始化_内存太小_结构体隐含指针

    1.指针没有指向一块合法的内存 定义了指针变量,但是没有为指针分配内存,即指针没有指向一块合法的内浅显的例子就不举了,这里举几个比较隐蔽的例子. 1.1结构体成员指针未初始化 struct stude ...

  9. void指针、NULL指针和未初始化指针

    一个指针可以被声明为void类型,比如void *x.一个指针可以被赋值为NULL.一个指针变量声明之后但没有被赋值,叫做未初始化指针. 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

随机推荐

  1. 大家都能看得懂的源码 - 那些关于DOM的常见Hook封装(一)

    本文是深入浅出 ahooks 源码系列文章的第十四篇,该系列已整理成文档-地址.觉得还不错,给个 star 支持一下哈,Thanks. 上一篇我们探讨了 ahooks 对 DOM 类 Hooks 使用 ...

  2. HCNP Routing&Switching之DHCP安全

    前文我们了解了MAC地址防漂移技术,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16632239.html:今天我们来了解下DHCP安全相关话题: 回顾DHC ...

  3. OSI七层模型与TCP/IP协议

    作者:菘蓝 时间:2022/9/1 ================================================================================== ...

  4. format添加未知个参数方法

    一个python巧妙技巧,分享给大家 我的需求是将一个dict的键都format输出,用到了*对字典解包 data = {a: 1, b: 2...} msg = f"{'{} '*len( ...

  5. 播放器之争:VLC还是martPlayer

    好多开发者跟我们交流的时候提到,为什么有了VLC这种开源播放器,大牛直播SDK还要开发SmartPlayer?以下就针对VLC和SmartPlayer功能支持和涉及侧重,做个大概的比较: VLC VL ...

  6. IK分词器实现原理剖析 —— 一个小问题引发的思考

    前言: 网上很多的文章都建议在使用IK分词器的时候,建立索引的时候使用ik_max_word模式:搜索的时候使用ik_smart模式.理由是max_word模式分词的结果会包含smart分词的结果,这 ...

  7. 授予用户/用户组访问 Kubernetes 的一个名称空间

    转载地址:https://www.kuboard.cn/learning/k8s-advanced/sec/rbac/auth-namespace.html 前提条件 已安装 Kuboard v3,版 ...

  8. centos7使用yum方式安装redis6

    yum -y install epel-release wget make gcc-c++ cd /opt wget https://download.redis.io/releases/redis- ...

  9. NetworkPolicy网络策略以及举例说明

    网络策略(NetworkPolicy)是一种关于pod间及pod与其他网络端点间所允许的通信规则的规范.NetworkPolicy 资源使用标签选择pod,并定义选定pod所允许的通信规则. 前提 网 ...

  10. elk使用微信ElartAlert企业微信告警,自定义告警内容

    第一种方式 alert: - "elastalert_modules.wechat_qiye_alert.WeChatAlerter" alert_text: " === ...