将参数设置到配置节(WPF)时的System.TypeInitializationException

时间:2016-05-13 10:55:14

标签: c# wpf app-config

我有一个WPF应用程序,它从我的App.Config文件中读取数据。 App.Config看起来像:

<configuration>
[...]
  <MySection>
    <application>
      <general displayConsole="true"/>
     <!-- <general displayDebug="false"/> -->
    </application>
  </MySection>
</configuration>

我使用此类访问该部分:

   public class ApplicationSetting : AbstractSetting
    {
        private const string SECTION_GROUP = "MySection";
        private const string SECTION_APPLICATION = "application";

        [ConfigurationProperty("general", IsRequired = true)]
        public GeneralElement General { get { return (GeneralElement)this["general"]; } set { this["general"] = value; } }

        public static ApplicationSetting Load()
        {
            return (ApplicationSetting)Configuration.GetSection(SECTION_GROUP + "/" + SECTION_APPLICATION);
        }
    }

    public class GeneralElement : ConfigurationElement
    {
        [ConfigurationProperty("displayConsole", IsRequired = true)]
        public bool DisplayConsole { get { return (bool)this["displayConsole"]; } set { this["displayConsole"] = value; } }
/*
        [ConfigurationProperty("displayDebug", IsRequired = true)]
        public bool DisplayDebug { get { return (bool)this["displayDebug"]; } set { this["displayDebug"] = value; } }*/
    }

现在,如果我想向此应用添加第二个参数并取消注释注释行,我会收到以下异常:

  

&#39; System.TypeInitializationException&#39;成   Microsoft.VisualStudio.HostingProcess.Utilities.dll

为什么这样?我怀疑一个可耻的XML问题?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

确实是一个令人遗憾的XML问题。 如果有人关心,正确的语法是:

  <valeoConfigs>
    <application>
      <general displayConsole="true"  displayDebug="false"/>
    </application>
  </valeoConfigs>