当前位置:  开发笔记 > 编程语言 > 正文

访问.Net中的自定义配置部分

如何解决《访问.Net中的自定义配置部分》经验,为你挑选了2个好方法。

我正在尝试访问配置文件中的设置,这是一系列xml元素,如下所示:





现在我想访问它.我已经设置了这样的类:

Public Class DatabaseConfigurationHandler
    Inherits ConfigurationSection

     _
   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

     _
       Public Property DatabaseName() As String
        Get
            Return Me("name").ToString()
        End Get
        Set(ByVal Value As String)
            Me("name") = Value
        End Set
    End Property


     _
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

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

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

我错过了一些代码,我做错了什么?上面的任何其他错误?

谢谢.



1> casperOne..:

手工设计这种东西没有任何充分的理由了.相反,您应该在CodePlex上使用配置部分设计器:

http://csd.codeplex.com/

安装后,您只需向项目中添加一个新项目(配置节设计器),然后添加元素和约束.我发现它非常容易使用,我可能永远不会再为配置文件编写一段代码.



2> Steven Robbi..:

这是我几天前做的非常相似的剪切和粘贴.

配置:

  
    
      
      
    
  

配置部分C#:

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

namespace App
{
    /// 
    /// Individual list configuration
    /// 
    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"]; }
        }
    }

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

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

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

以及从主应用程序中获取它的代码:

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


好问题,我不需要为这个特定的部分,但你可以用LINQ:ListConfiguration li = configSection.Lists.OfType ().Where(l => l.Name =="blah") .FirstOrDefault();
推荐阅读
小白也坚强_177
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有