ConfigurationProperty的典型示例 [英] Canonical example of ConfigurationProperty

查看:32
本文介绍了ConfigurationProperty的典型示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试利用.NET配置并了解自定义部分,元素等.

I am trying to make use of .NET configuration and understand custom sections, elements etc.

实现这些自定义部分似乎需要显式声明getter和setter,通常会导致代码膨胀.

It seems that implementing these custom sections requires explicit declaration of getters and setters, generally resulting in code bloat.

例如此处:

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

特别是,我们似乎有必要在get和set方法中显式返回并设置事物.

Specifically, it seems necessary for us to explicitly return and set things in the get and set methods.

// Create a "remoteOnly" attribute.
[ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
public Boolean RemoteOnly
{
  get
  { 
    return (Boolean)this["remoteOnly"]; 
  }
  set
  { 
    this["remoteOnly"] = value; 
  }
}

具有以下

[ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
public Boolean RemoteOnly { get; set }

不等同于上述内容.

这是真的吗?即使具有这种香草特性,我们也必须冗长吗?

Is this really true - do we have to be verbose even with such vanilla properties?

推荐答案

是的,因为您依赖外部存储机制(基类的字典,该字典最终填充了配置文件).

Yes, because you're relying on an external storage mechanism (the base class's dictionary which ends up populating the config file).

此外,如果您担心此问题会导致代码膨胀,那么您还会担心错误的事情.只写一次,再也不要看了.您应该没有太多的代码来处理自定义配置设置,以至于会使您的代码库肿.可能会有微小的颠簸,但不会肿胀.另外,我很高兴您现在就开始处理这个问题,而不是在VS2005之前-您还必须编写很多代码(必须手动解析XML部分).

Also, if you're worried about this for code bloat, you are worrying about the wrong things. Write it once, never look at it again. You shouldn't have so much code dealing with custom configuration settings that it will bloat your codebase. A teeny bump maybe, but not bloat. Also, I'd be happy you're dealing with this now and not before VS2005 - there was a LOT more code you had to write (you had to parse the XML sections manually).

最后,如果您仍然讨厌它,可以随时使用我将需要的最后一个配置节处理程序": https://sites.google.com/site/craigandera/craigs-stuff/clr-workings/the-last-configuration-section-handler-i-ll-ever-need

Finally, if you still hate it that much, you can always use "the last configuration section handler I'll ever need": https://sites.google.com/site/craigandera/craigs-stuff/clr-workings/the-last-configuration-section-handler-i-ll-ever-need

自从他写那本书已经很久了,但是它应该仍然可以正常工作.

It's been a long time since he wrote that, but it should still work just fine.

这篇关于ConfigurationProperty的典型示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆