什么是“配置类型”?
我在与配置文件交互方面没有太多经验,我正在阅读 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 技术交流群。
发布评论
评论(2)
睫毛上残留的泪2024-12-12 20:44:06
MSDN 上有一些很棒的示例:http://msdn.microsoft.com/en -us/library/2tw134k3.aspx
这里,“配置类型”是为扩展 ConfigurationSection
而创建的自定义类型。是的,这是作为 XML 节点实现的,但 System.Configuration 命名空间的目的是将其抽象化。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
配置类型基本上只是您定义的自定义类的类型,用于表示您想要存储在 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 中,例如:
任何时候我想要访问该属性,我都会在代码中执行以下操作:
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 theGetSection
method, you need to cast the return value as the type of your Custom class that you inherited off ofSystem.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:
Any time I would want to access that property I would do the following in my code: