加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么ConfigurationSection需要用字符串查找?

发布时间:2020-12-15 23:46:50 所属栏目:百科 来源:网络整理
导读:我在网上找到的ConfigurationSection示例( for example)都有如下代码: public class ConnectionSection : ConfigurationSection{ [ConfigurationProperty("Servers")] public ServerAppearanceCollection ServerElement { get { return ((ServerAppearanceC
我在网上找到的ConfigurationSection示例( for example)都有如下代码:

public class ConnectionSection  : ConfigurationSection
{
    [ConfigurationProperty("Servers")]
    public ServerAppearanceCollection ServerElement
    {
        get { return ((ServerAppearanceCollection)(base["Servers"])); }
        set { base["Servers"] = value; }
    }
}

为什么使用方括号从基数访问值“Servers”?是从xml创建此对象时使用的setter,还是用于覆盖xml文件中的值的setter?如果是这样,为什么在此属性上设置属性?

解决方法

Why is it accessing the value “Servers” from the base using the square brackets?

因为基类ConfigurationSection不知道它的继承者将实现什么属性.

因此它暴露了a string indexer: this[string],它允许您访问从配置中读取的值.

这是一个设计决定. .NET团队也可以选择使用反射来获取和设置继承者的属性,但决定不这样做. (当然,配置部分中有很多反映,但直到公共ServerAppearanceCollection ServerElement {get; set;}才能工作).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读