c# – 无法强制转换为我的自定义ConfigurationSection
发布时间:2020-12-16 01:49:35 所属栏目:百科 来源:网络整理
导读:我正在尝试读取这样的配置文件: var dllPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly. GetExecutingAssembly().GetName().CodeBase);dllPath = dllPath.Replace("file:",string.Empty);var configPath = string.Format(@"{0}..c
我正在尝试读取这样的配置文件:
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"); 我有在CorpCommon项目中定义的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="contentimages"/> <contentFolder key="CssFolder" path="contentcss"/> ... </contentFolders> </contentFolderConfiguration> </configuration> 但调用config.GetSection()的行正在抛出InvalidCastException: Unable to cast object of type 'System.Configuration.DefaultSection' to type 'Corp.Common.ContentFolderSettings'. 解决方法
标签丢失了
在我的情况下是这样的: <configSections> 所以你只需添加标签 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |