C# 自定义配置节.NET

C# 自定义配置节.NET,c#,web-config,configurationmanager,C#,Web Config,Configurationmanager,我正在尝试创建一个自定义配置节,用于保存以下教程中的一些API凭据:。我已将以下内容添加到我的Web.config文件中: <!-- Under configSections --> <section name="providers" type="EmailApiAccess.Services.ProviderSection, EmailApiAccess.Services"/> <!-- Root Level --> <providers>

我正在尝试创建一个自定义配置节,用于保存以下教程中的一些API凭据:。我已将以下内容添加到我的Web.config文件中:

<!-- Under configSections -->
 <section name="providers" type="EmailApiAccess.Services.ProviderSection, EmailApiAccess.Services"/>

<!-- Root Level -->
<providers>
    <add name="ProviderName" username="" password ="" host="" />
  </providers>
但是,当我尝试使用以下方法实现代码时:

var section = ConfigurationManager.GetSection("providers");
ProviderElement element = section.Providers["ProviderName"];
var creds = new NetworkCredential(element.username, element.password);   
我得到一个错误,说“对象”不包含“提供者”的定义

任何帮助都将不胜感激

返回类型为
object
的值,而不是自定义配置节。请尝试以下操作:

var section = ConfigurationManager.GetSection("providers") as ProviderSection;
var section = ConfigurationManager.GetSection("providers") as ProviderSection;