自定义App.config配置读取失败:类型加载异常求助
解决自定义配置加载失败的问题
我帮你排查了这个自定义配置加载失败的问题,结合你的代码和配置文件,找到了几个关键错误点和对应的修复方案:
核心问题分析与修复步骤
1. 配置文件中type属性格式错误
配置节的type属性必须遵循「完整类名, 程序集名称」的格式,你当前写的StockCSV.CordnersConfiguration.CordnerConfigurationSection, StockCSV.CordnersConfiguration里,后半段用了命名空间而非程序集名称——程序集名称是你项目编译后生成的dll/exe的名字(通常和项目名一致,比如你的项目叫StockCSV,那程序集名就是StockCSV)。
修正后的type值:
type="StockCSV.CordnersConfiguration.CordnerConfigurationSection, StockCSV"
2. 配置属性与代码定义不匹配
你的配置文件里cordnerConfig节点用的是Location属性,但C#代码里定义的ConfigurationProperty是outputPath,两者名称完全不匹配,导致框架无法正确绑定配置值。另外你代码里返回的OutputConfigurationElement类型并没有定义,如果你只是要读取一个简单的路径字符串,完全可以简化代码,不需要自定义元素。
3. 静态初始化可能引发的问题
你当前用静态字段直接初始化配置,可能会在程序启动初期就触发配置读取,此时配置系统可能还未完全初始化,改成延迟加载会更稳妥。
修正后的完整代码与配置
修正后的App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="CordnerConfigurationSection"> <!-- 修正程序集名称 --> <section name="cordnerConfig" type="StockCSV.CordnersConfiguration.CordnerConfigurationSection, StockCSV" allowLocation="false" allowDefinition="Everywhere" /> </sectionGroup> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup> <CordnerConfigurationSection> <!-- 属性名称和代码里的ConfigurationProperty保持一致 --> <cordnerConfig outputPath="C:\Users\Conor\Desktop\Cordners Data Dump\stocknew.csv"></cordnerConfig> </CordnerConfigurationSection> </configuration>
修正后的C#代码
namespace StockCSV.CordnersConfiguration { public class CordnerConfigurationSection : ConfigurationSection { private static CordnerConfigurationSection _cordnerConfig; // 延迟加载配置,避免初始化时机问题 public static CordnerConfigurationSection CordnerConfig { get { if (_cordnerConfig == null) { _cordnerConfig = (CordnerConfigurationSection)System.Configuration.ConfigurationManager.GetSection("CordnerConfigurationSection/cordnerConfig"); } return _cordnerConfig; } } // 配置属性名称和配置文件里的保持一致 private const string OutputPathKey = "outputPath"; // 直接用string类型读取路径,无需自定义元素 [ConfigurationProperty(OutputPathKey, IsRequired = true)] public string OutputLocation { get => (string)this[OutputPathKey]; set => this[OutputPathKey] = value; } } }
可选:如果需要自定义配置元素
如果你未来要给这个配置节点扩展更多属性,可以定义OutputConfigurationElement类:
public class OutputConfigurationElement : ConfigurationElement { [ConfigurationProperty("Location", IsRequired = true)] public string Location { get => (string)this["Location"]; set => this["Location"] = value; } }
此时需要修改主配置类的属性类型,并调整配置文件结构:
// 主配置类里的属性修改为: [ConfigurationProperty(OutputPathKey, IsRequired = true)] public OutputConfigurationElement OutputLocation { get => (OutputConfigurationElement)this[OutputPathKey]; set => this[OutputPathKey] = value; }
对应的配置文件结构:
<CordnerConfigurationSection> <cordnerConfig> <outputPath Location="C:\Users\Conor\Desktop\Cordners Data Dump\stocknew.csv"></outputPath> </cordnerConfig> </CordnerConfigurationSection>
额外注意事项
- 确认项目的程序集名称:右键项目 → 属性 → 应用程序标签,查看“程序集名称”是否为StockCSV,这必须和配置文件里type属性的后半段一致。
- 确保App.config复制到输出目录:右键App.config → 属性,设置“复制到输出目录”为“始终复制”或“如果较新则复制”,避免运行时找不到配置文件。
- 调试时可以先判断
CordnerConfig是否为null,方便快速定位问题。
内容的提问来源于stack exchange,提问作者Conor Shannon
相关产品推荐
相关产品推荐

