什么是“配置类型”?

发布于 12-05 20:44 字数 349 浏览 0 评论 0原文

我在与配置文件交互方面没有太多经验,我正在阅读 MSDN 中的 GetSection() 方法,其中指出:

**Notes to Implementers**: 

    You must cast the return value to the expected configuration type. 
To avoid possible casting exceptions, you should use a conditional 
casting operation such as... 

本说明中的“配置类型”是什么意思?选定的部分不是始终代表 xml 节点吗?

I don't have much experience on interactiong with config files and I was reading the GetSection() method in MSDN which notes that:

**Notes to Implementers**: 

    You must cast the return value to the expected configuration type. 
To avoid possible casting exceptions, you should use a conditional 
casting operation such as... 

What does "configuration type" mean in this note? Are not the sections selected represents an xml node always?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你2024-12-12 20:44:06

配置类型基本上只是您定义的自定义类的类型,用于表示您想要存储在 App.Config 或 Web.Config 中的配置值

您的自定义配置部分需要继承自 System.Configuration.ConfigurationSection 当您使用 GetSection 方法时,您需要将返回值转换为您从 System.Configuration.ConfigurationSection 继承的自定义类的类型

查看更多 <一个href="http://msdn.microsoft.com/en-us/library/2tw134k3.aspx" rel="nofollow">此处

一个例子是,如果我有一个特殊的类来表示我想要的属性存储在 App.Config 或 Web.Config 中,例如:

public class MyConfig : ConfigurationSection
{
    [ConfigurationProperty("myConfigProp", DefaultValue = "false", IsRequired = false)]
    public Boolean MyConfigProp
    {
        get
        { 
            return (Boolean)this["myConfigProp"]; 
        }
        set
        { 
            this["myConfigProp"] = value; 
        }
    }
}

任何时候我想要访问该属性,我都会在代码中执行以下操作:

//create a MyConfig object from the XML in my App.Config file
MyConfig config = (MyConfig)System.Configuration.ConfigurationManager.GetSection("myConfig");

//access the MyConfigProp property
bool test = config.MyConfigProp;

Configuration Type is basically just the type of the custom class you define to represent the configuration values you want to store in the App.Config or Web.Config

Your custom config section needs to inherit from System.Configuration.ConfigurationSection and when you use the GetSection method, you need to cast the return value as the type of your Custom class that you inherited off of System.Configuration.ConfigurationSection

see more here

An example would be if I had a special class to represent a property I wanted to store in either the App.Config or Web.Config such as:

public class MyConfig : ConfigurationSection
{
    [ConfigurationProperty("myConfigProp", DefaultValue = "false", IsRequired = false)]
    public Boolean MyConfigProp
    {
        get
        { 
            return (Boolean)this["myConfigProp"]; 
        }
        set
        { 
            this["myConfigProp"] = value; 
        }
    }
}

Any time I would want to access that property I would do the following in my code:

//create a MyConfig object from the XML in my App.Config file
MyConfig config = (MyConfig)System.Configuration.ConfigurationManager.GetSection("myConfig");

//access the MyConfigProp property
bool test = config.MyConfigProp;
睫毛上残留的泪2024-12-12 20:44:06

MSDN 上有一些很棒的示例:http://msdn.microsoft.com/en -us/library/2tw134k3.aspx

这里,“配置类型”是为扩展 ConfigurationSection 而创建的自定义类型。是的,这是作为 XML 节点实现的,但 System.Configuration 命名空间的目的是将其抽象化。

There's some great samples on MSDN here: http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

Here, the "configuration type" is the custom type that's been created to extend ConfigurationSection. Yes, this is implemented as an XML node, but the intent of the System.Configuration namespace is to abstract this away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文