Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用自定义配置节获取app.config值。。。?_C#_Nullreferenceexception_Configsection - Fatal编程技术网

C# 如何使用自定义配置节获取app.config值。。。?

C# 如何使用自定义配置节获取app.config值。。。?,c#,nullreferenceexception,configsection,C#,Nullreferenceexception,Configsection,我想获取服务器地址值,但在警报方法中收到一个null引用异常: Values.Server.Key[“CollectorServer”]。地址->空引用 例外情况 我的app.config如下所示: <configSections> <section requirePermission="false" name="serverlist" type="SampleConsole.CustomAppTest, SampleConsole"></section>

我想获取服务器地址值,但在警报方法中收到一个
null引用异常

Values.Server.Key[“CollectorServer”]。地址->空引用 例外情况

我的app.config如下所示:

<configSections>
  <section requirePermission="false"  name="serverlist" type="SampleConsole.CustomAppTest, SampleConsole"></section>
</configSections>

<serverlist>
  <add name="CollectorServer" address="127.0.0.1"></add>
</serverlist>
namespace SampleConsole
{
public class CustomAppTest
{
    public void Alert()
    {

        Console.WriteLine(Values.Server.Key["CollectorServer"].Address);
    }
}


public class Values
{

    public static ServerValues Server = ConfigurationManager.GetSection("serverlist") as ServerValues;

}


public class ServerValues : ConfigurationSection
{

    [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
    public ServerCollection Key
    {
        get { return (ServerCollection)this[""]; }
        set { this[""] = value; }
    }
}


public class ServerCollection : ConfigurationElementCollection
{

    protected override ConfigurationElement CreateNewElement()
    {
        return new ServerElement();
    }


    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ServerElement)element).Name;
    }


    public new ServerElement this[string elementName]
    {
        get
        {
            return this.OfType<ServerElement>().FirstOrDefault(item => item.Name == elementName);
        }
    }
}

public class ServerElement : ConfigurationElement
{

    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }


    [ConfigurationProperty("address", IsRequired = true)]
    public string Address
    {
        get { return (string)base["address"]; }
        set { base["address"] = value; }
    }
}}

我的自定义配置部分如下所示:

<configSections>
  <section requirePermission="false"  name="serverlist" type="SampleConsole.CustomAppTest, SampleConsole"></section>
</configSections>

<serverlist>
  <add name="CollectorServer" address="127.0.0.1"></add>
</serverlist>
namespace SampleConsole
{
public class CustomAppTest
{
    public void Alert()
    {

        Console.WriteLine(Values.Server.Key["CollectorServer"].Address);
    }
}


public class Values
{

    public static ServerValues Server = ConfigurationManager.GetSection("serverlist") as ServerValues;

}


public class ServerValues : ConfigurationSection
{

    [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
    public ServerCollection Key
    {
        get { return (ServerCollection)this[""]; }
        set { this[""] = value; }
    }
}


public class ServerCollection : ConfigurationElementCollection
{

    protected override ConfigurationElement CreateNewElement()
    {
        return new ServerElement();
    }


    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ServerElement)element).Name;
    }


    public new ServerElement this[string elementName]
    {
        get
        {
            return this.OfType<ServerElement>().FirstOrDefault(item => item.Name == elementName);
        }
    }
}

public class ServerElement : ConfigurationElement
{

    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }


    [ConfigurationProperty("address", IsRequired = true)]
    public string Address
    {
        get { return (string)base["address"]; }
        set { base["address"] = value; }
    }
}}
名称空间示例控制台
{
公共类CustomAppTest
{
公众虚空警报()
{
Console.WriteLine(Values.Server.Key[“CollectorServer”].Address);
}
}
公共阶级价值观
{
public static ServerValues Server=ConfigurationManager.GetSection(“serverlist”)作为ServerValues;
}
公共类ServerValues:ConfigurationSection
{
[ConfigurationProperty(“,IsRequired=true,IsDefaultCollection=true)]
公共服务器集合密钥
{
获取{return(ServerCollection)this[“”];}
设置{this[“”]=value;}
}
}
公共类ServerCollection:ConfigurationElementCollection
{
受保护的覆盖ConfigurationElement CreateNewElement()
{
返回新的ServerElement();
}
受保护的覆盖对象GetElementKey(ConfigurationElement元素)
{
return((ServerElement)元素).Name;
}
public new ServerElement this[string elementName]
{
得到
{
返回此.OfType().FirstOrDefault(item=>item.Name==elementName);
}
}
}
公共类ServerElement:ConfigurationElement
{
[配置属性(“名称”,IsKey=true,IsRequired=true)]
公共字符串名
{
获取{return(string)base[“name”];}
集合{base[“name”]=value;}
}
[配置属性(“地址”,IsRequired=true)]
公共字符串地址
{
获取{return(string)base[“address”];}
设置{base[“address”]=value;}
}
}}
试试这个。。。 让你的应用程序配置如下

<serverfulllist>
    <serverlist>
      <add name="CollectorServer" value="127.0.0.1"/>
    </serverlist>
</serverfulllist>
NameValueCollection address =  
ConfigurationManager.GetSection("serverfulllist/serverlist")
as System.Collections.Specialized.NameValueCollection;

if (address != null)
{
    foreach (string key in address.AllKeys)
    {
       Response.Write(key + ": " + address[key] + "<br />");
    }
}

NameValueCollection地址=
ConfigurationManager.GetSection(“serverfulllist/serverlist”)
作为System.Collections.Specialized.NameValueCollection;
if(地址!=null)
{
foreach(地址中的字符串键.AllKeys)
{
响应.写入(键+“:“+地址[键]+”
); } }
if(address!=null){foreach(address.AllKeys中的字符串键){Response.Write(key+”:“+address[key]+”
;}}}else{console.Write(“无对象…”);签出,如果您所做的只是添加一个键/值设置类型,那么只需使用appsettings配置部分就可以了