C# 在app.config中添加自定义节后System.InvalidCastException
我想创建一个简单的部分,而不必自己编写任何类,我已经阅读了其他帖子,并尝试构建自己的部分,但当我尝试获取我的部分时,它抛出System.InvalidCastException异常。有人能告诉我怎么解决吗?谢谢 异常消息: HttpServer.exe中发生类型为“System.InvalidCastException”的未处理异常 其他信息:无法将“System.Configuration.KeyValueInternalCollection”类型的对象强制转换为“System.Configuration.AppSettingsSection”类型 App.config:C# 在app.config中添加自定义节后System.InvalidCastException,c#,app-config,C#,App Config,我想创建一个简单的部分,而不必自己编写任何类,我已经阅读了其他帖子,并尝试构建自己的部分,但当我尝试获取我的部分时,它抛出System.InvalidCastException异常。有人能告诉我怎么解决吗?谢谢 异常消息: HttpServer.exe中发生类型为“System.InvalidCastException”的未处理异常 其他信息:无法将“System.Configuration.KeyValueInternalCollection”类型的对象强制转换为“System.Configu
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="extensions" type="System.Configuration.AppSettingsSection" />
</configSections>
<extensions>
<add key=".gif" value="image/gif"/>
<add key=".png" value="image/png"/>
<add key=".jpg" value="image/jpeg"/>
</extensions>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
将System.Configuration.AppSettings选项更改为System.Configuration.NameValueSectionHandler,并通过 System.Collections.Specialized.NameValueCollection
AppSettingsSection section = (AppSettingsSection)ConfigurationManager.GetSection("extensions");
Console.WriteLine(section.Settings[".gif"].Value.ToString());