无法转换为我的自定义 ConfigurationSection

发布于 2024-12-18 19:49:09 字数 1503 浏览 4 评论 0原文

我正在尝试读取这样的配置文件:

var dllPath =  System.IO.Path.GetDirectoryName(System.Reflection.Assembly.
    GetExecutingAssembly().GetName().CodeBase);
dllPath = dllPath.Replace("file:\\", string.Empty);
var configPath = string.Format(@"{0}\..\contentFolders.config", dllPath);
var fileMap = new ExeConfigurationFileMap() {ExeConfigFilename = configPath};
var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, 
    ConfigurationUserLevel.None);
var contentFolderConfig = 
    (ContentFolderSettings)config.GetSection("contentFolderConfiguration");

我在 Corp.Common 项目中定义了 ContentFolderSettings,并且它继承自 ConfigurationSection。以下是 contentFolders.config 的内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <section name="contentFolderConfiguration"
    type="Corp.Common.ContentFolderSettings, Corp.Common"
    requirePermission="false"/>
  <contentFolderConfiguration>
    <contentFolders>
      <contentFolder key="ImagesFolder" path="content\images"/>
      <contentFolder key="CssFolder" path="content\css"/>
      ...
    </contentFolders>
  </contentFolderConfiguration>
</configuration>

但是调用 config.GetSection() 的行抛出 InvalidCastException

Unable to cast object of type 'System.Configuration.DefaultSection' to type 
'Corp.Common.ContentFolderSettings'.

I'm trying to read a config file like this:

var dllPath =  System.IO.Path.GetDirectoryName(System.Reflection.Assembly.
    GetExecutingAssembly().GetName().CodeBase);
dllPath = dllPath.Replace("file:\\", string.Empty);
var configPath = string.Format(@"{0}\..\contentFolders.config", dllPath);
var fileMap = new ExeConfigurationFileMap() {ExeConfigFilename = configPath};
var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, 
    ConfigurationUserLevel.None);
var contentFolderConfig = 
    (ContentFolderSettings)config.GetSection("contentFolderConfiguration");

I have ContentFolderSettings defined in the Corp.Common project and it inherits from ConfigurationSection. Here is the contents of contentFolders.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <section name="contentFolderConfiguration"
    type="Corp.Common.ContentFolderSettings, Corp.Common"
    requirePermission="false"/>
  <contentFolderConfiguration>
    <contentFolders>
      <contentFolder key="ImagesFolder" path="content\images"/>
      <contentFolder key="CssFolder" path="content\css"/>
      ...
    </contentFolders>
  </contentFolderConfiguration>
</configuration>

But the line calling config.GetSection() is throwing InvalidCastException on:

Unable to cast object of type 'System.Configuration.DefaultSection' to type 
'Corp.Common.ContentFolderSettings'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的十字路口 2024-12-25 19:49:09

标签丢失

在我的例子中是这样的:

<configSections>

所以你只需添加标签

在此处输入图像描述

tag is missing

In my case was this:

<configSections>

so you just add the tag

enter image description here

走走停停 2024-12-25 19:49:09

标签丢失。

包含配置节和命名空间声明。

http://msdn.microsoft.com/en-我们/library/aa903350(v=vs.71).aspx

Tag is missing .

Contains configuration section and namespace declarations.

http://msdn.microsoft.com/en-us/library/aa903350(v=vs.71).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文