C#的自定义配置

C#的自定义配置,第1张

概述我有一个自定义配置的App.config <?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="wsa" type="WSASRT.SRT.WSA.WsaConfig" /> </configSections> <startup> <support 我有一个自定义配置的App.config

<?xml version="1.0" enCoding="utf-8" ?><configuration>  <configSections>    <section name="wsa" type="WSASRT.SRT.WSA.WsaConfig" />  </configSections>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  </startup>  <wsa>    <src E="Foo@bar.com" CN="AnotherFoo" OU="Anotherbar" />    <!-- More elements -->  </wsa></configuration>

以及映射类

namespace WSASRT.SRT.WSA{    class WsaConfig : ConfigurationSection    {        [ConfigurationProperty("src")]        public SrcElement Src { get { return (SrcElement)this["src"]; } }    }    public class SrcElement : ConfigurationElement    {        [ConfigurationProperty("E")]        public string E { get { return (String)this["E"]; } }        [ConfigurationProperty("CN")]        public string CN { get { return (String)this["CN"]; } }    }}

我的Program.cs看起来像:

class Program{    static voID Main(string[] args)    {        WsaConfig config = new WsaConfig();        Console.Writeline(config.Src.CN);        Console.Readline();        }    }

当我运行它时,我得到一个空字符串,但我应该得到“AnotherFoo”.我究竟做错了什么?

解决方法 替换下面的第一行,以便从配置文件&中读取它.没有实例化新的..

WsaConfig config = ConfigurationManager.GetSection("wsa") as WsaConfig;
总结

以上是内存溢出为你收集整理的C#的自定义配置全部内容,希望文章能够帮你解决C#的自定义配置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1231336.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存