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

2023-11-25

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

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

现在我想访问它。我已经设置了这样的课程:

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

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

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

我是否缺少一些代码,我做错了什么?以上还有其他错误吗?

Thanks.


这是我几天前所做的非常相似的事情的剪切和粘贴。

Config:

  <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"]; }
        }
    }
}

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

ListConfigurationSection configSection = null;
try
{
    configSection = ConfigurationManager.GetSection("ListConfigurations") as ListConfigurationSection;
}
catch (System.Configuration.ConfigurationErrorsException)
{
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

访问 .Net 中的自定义配置部分 的相关文章

  • 自动加载 linq2entities 中的关系

    当我的模型中的两个实体之间存在关系时 组成员 1 用户 并尝试使用 LINQ 从该关系中选择项目 从 user GroupMember 中的实体选择实体 除非我首先使用以下语句加载关系 否则我总是得到空结果 user GroupMember
  • 将列表拆分为多个部分 - VB 转换失败

    尝试编写一种将列表拆分为子列表的方法 Private Function SplitIdsIntoChunks ByVal keys As List Of String As List Of List Of String Return key
  • 拆分容器,制作固定面板

    我有一个水平方向的 splitcontainer 我希望仅在表单调整大小期间为 panel2 设置固定高度 并让 splitter 调整 panel2 大小 现在我正在这样做 但我不满意 因为用户注意到面板调整了大小 Private Sub
  • 如何将 POST 请求内容保存为 .NET 中的文件 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我有一个客户端应用程序POST请求a
  • 我的程序的“服务”应该负责获取自己的配置吗?

    我的程序由几个 服务 组成 我不是在谈论SOA http en wikipedia org wiki Service oriented architecture 所以不要混淆 并且每个 服务 都使用一些配置 例如 目录路径 该配置应该从配置
  • 将 C# Web 表单添加到 VB Web 应用程序

    有没有办法将 C Web 窗体添加到用 VB NET 编写的 ASP NET 4 0 Web 应用程序中 我有一个 ASP NET 2 0WEB SITE我最近转换为 ASP NET 4 0WEB应用程序 它主要是用 VB NET 编写的
  • 忽略不区分大小写的字典键中的连字符

    我在 asp net vb net 中有一个不区分大小写的字典 如下所示 Dim caseInsensitiveDictionary New Dictionary Of String Single StringComparer Ordina
  • 在word中添加超链接,使用vb.net

    我目前正在尝试通过 VB 程序在 word 中添加一个指向 web url 的超链接 我正在跌跌撞撞地尝试找到正确的语法以及完成此任务所需的内容 因为我收到了很多无用的 VBA 示例 而这根本不是我需要的 我的代码如下所示 sPara2 o
  • VB.NET“Like”运算符中可能存在错误?

    为什么下面的评价为True Dim result b Like a b Thanks EDIT 为了概括这一点 以下返回True String1 Like AnyText1 AnyText2 AnyText String1 VBA 工作正常
  • 在本地安全存储用于 Web 服务的密码

    我有一个应用程序 通过发送用户名和密码来对第三方 Web 服务进行身份验证 目前 我每次启动应用程序时都会在 winform 上输入密码 但我需要它自动登录 我想比更安全地存储用户名 密码 Dim username as String us
  • “更新/取消”按钮不会出现在模板字段编辑按钮中

    当您使用 Gridview 的每一行创建编辑按钮时CommandField单击后它会显示更新 取消按钮 以便您可以接受 取消更改 但是 我想要一个带有工具提示文本的编辑按钮 因为CommandField没有工具提示属性 我用过Templat
  • 是否可以为 Visual Studio 2010 编写一个调试器可视化工具来显示 64 位 .NET 程序?

    是否可以为 Visual Studio 2010 编写一个调试器可视化工具来显示 64 位 NET 程序的数据 我已经为 32 位编程编写了它们 但我无法设法使用或编译它们以用于 64 位应用程序 有什么建议么 我刚刚经历过同样的问题 我使
  • 如何判断一个类是否被某个特定属性修饰

    我试图确定接口是否用特定属性装饰 例如我有以下界面
  • Datagridview 单元格焦点

    我有一个从数据库加载数据的数据网格视图 这是未绑定的 datagridview 这些列是描述 价格 数量和总计 说明 U价格来自数据库 然后输入数量 我希望这样当我的数据网格加载时 光标会转到 数量 列 并且它会像我们在文本框中那样闪烁显示
  • Visual Studio 多个启动项目之间存在延迟?

    如何在解决方案中的启动项目之间添加一些延迟 我希望客户端项目在启动 WindowsService 后 2 3 秒后启动 为什么我需要这个 WindowsService运行socket服务器 Client运行socket来连接服务器 Wind
  • 动态版本控制

    我有一种情况 我希望版本控制在构建时是动态的 版本图案
  • 将 .NET P/Invoke 代码组织为 Win32 API 的最佳实践

    我正在 NET 中重构一个大型且复杂的代码库 该代码库大量使用 P Invoke to Win32 API 该项目的结构不是最好的 我发现 DllImport 语句遍布各处 经常为同一函数重复 并且还以多种方式声明 导入指令和方法有时声明为
  • 在javascript中访问隐藏字段值

    我的表单中有一个隐藏字段 我正在服务器上设置隐藏字段的值并尝试从 javascript 访问该值 我收到错误 无法获取属性 值 的值 对象为 null 或未定义 如果我查看源代码 则会设置隐藏字段值 并且隐藏字段的 ID 与我正在调用的 I
  • SELECT 语句会受到 SQL 注入攻击吗?

    实际上有2个问题 我知道我必须尽可能多地使用存储过程 但我想知道以下内容 A 我可以从 SELECT 语句 例如 Select from MyTable 获得 SQL 注入攻击吗 B 另外 当我在 ASP NET 中使用 SQLDataSo
  • 如何从 appsettings.json 文件中的对象数组读取值

    我的 appsettings json 文件 StudentBirthdays Anne 01 11 2000 Peter 29 07 2001 Jane 15 10 2001 John Not Mentioned 我有一个单独的配置类 p

随机推荐