не могу привести к моей пользовательской ConfigurationSection - PullRequest
3 голосов
/ 30 ноября 2011

Я пытаюсь прочитать файл конфигурации следующим образом:

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");

Я определил ContentFolderSettings в проекте Corp.Common, и он наследуется от 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'.

Ответы [ 2 ]

5 голосов
/ 11 июня 2013

тег отсутствует

В моем случае это было:

<configSections>

так что вы просто добавляете тег

enter image description here

0 голосов
/ 21 октября 2013

Метка отсутствует.

Содержит раздел конфигурации и объявления пространства имен.

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...