访问.Net中的自定义配置节 [英] Accessing a Custom Configuration Section in .Net

查看:187
本文介绍了访问.Net中的自定义配置节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试访问我的配置文件中的设置,这是一系列列出的xml元素:

I am trying to access settings in my config file, which is a series of xml elements listed as such:

<databases>
<database name="DatabaseOne" Value="[value]" />
<database name="DatabaseTwo" Value="[value]" />
</databases>

现在我想要访问它。我已经设置类如此:

Now I want to access it. I have set up classes like so:

Public Class DatabaseConfigurationHandler
    Inherits ConfigurationSection

    <ConfigurationProperty("Databases", IsDefaultCollection:=True)> _
   Public ReadOnly Property Databases() As DatabaseCollection
        Get
            Return CType(Me("Databases"), DatabaseCollection)
        End Get
    End Property
End Class

Public Class DatabaseCollection
    Inherits ConfigurationElementCollection

    Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
        Return (New Database())
    End Function

    Protected Overloads Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object
        Return (CType(element, Database).DatabaseName)
    End Function

End Class

Public Class Database
    Inherits ConfigurationElement

    <ConfigurationProperty("name", IsKey:=True, IsRequired:=True)> _
       Public Property DatabaseName() As String
        Get
            Return Me("name").ToString()
        End Get
        Set(ByVal Value As String)
            Me("name") = Value
        End Set
    End Property


    <ConfigurationProperty("value", IsRequired:=True)> _
Public Property DatabaseValue() As String
        Get
            Return Me("value").ToString()
        End Get
        Set(ByVal Value As String)
            Me("value") = Value
        End Set
    End Property

End Class

我想能够通过它的名字获取元素,并返回值,但我不能看到这样做:

I want to be able get the element by it's name and return the value but I can't see to do that:

Dim config As New DatabaseConfigurationHandler
                config = System.Configuration.ConfigurationManager.GetSection("databases/database")
                Return config.Databases("DatabaseOne")

我缺少一些代码,我做错了什么?上述任何其他错误?

Am I missing some code, what am I doing wrong? Any other errors in the above?

感谢。

推荐答案

配置:

  <ListConfigurations>
    <lists>
      <add Name="blah" EndpointConfigurationName="blah" ListName="blah" ConnectionString="blah" TableName="blah" FieldsCsv="blah" DbFieldsCsv="blah"/>
      <add Name="blah2" EndpointConfigurationName="blah" ListName="blah" ConnectionString="blah" TableName="blah" FieldsCsv="blah" DbFieldsCsv="blah"/>
    </lists>
  </ListConfigurations>

配置部分C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace App
{
    /// <summary>
    /// Individual list configuration
    /// </summary>
    class ListConfiguration : ConfigurationElement
    {
        [ConfigurationProperty("Name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)this["Name"]; }
        }

        [ConfigurationProperty("EndpointConfigurationName", IsRequired = true)]
        public string EndpointConfigurationName
        {
            get { return (string)this["EndpointConfigurationName"]; }
        }

        [ConfigurationProperty("ListName", IsRequired = true)]
        public string ListName
        {
            get { return (string)this["ListName"]; }
        }

        [ConfigurationProperty("ConnectionString", IsRequired = true)]
        public string ConnectionString
        {
            get { return (string)this["ConnectionString"]; }
        }

        [ConfigurationProperty("TableName", IsRequired = true)]
        public string TableName
        {
            get { return (string)this["TableName"]; }
        }

        [ConfigurationProperty("FieldsCsv", IsRequired = true)]
        public string FieldsCsv
        {
            get { return (string)this["FieldsCsv"]; }
        }

        [ConfigurationProperty("DbFieldsCsv", IsRequired = true)]
        public string DbFieldsCsv
        {
            get { return (string)this["DbFieldsCsv"]; }
        }
    }

    /// <summary>
    /// Collection of list configs
    /// </summary>
    class ListConfigurationCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ListConfiguration();
        }

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

    /// <summary>
    /// Config section
    /// </summary>
    class ListConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("lists")]
        public ListConfigurationCollection Lists
        {
            get { return (ListConfigurationCollection)this["lists"]; }
        }
    }
}



<它从主应用程序:

And the code to pick it up from the main app:

ListConfigurationSection configSection = null;
try
{
    configSection = ConfigurationManager.GetSection("ListConfigurations") as ListConfigurationSection;
}
catch (System.Configuration.ConfigurationErrorsException)
{
}

这篇关于访问.Net中的自定义配置节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆