简体   繁体   English

ConfigurationPropertyCollection 是否需要为 static?

[英]Does ConfigurationPropertyCollection need to be static?

According to this oft-referenced article, Unraveling the Mysteries of .NET Configuration , when implementing a ConfigurationSection / ConfigurationElement, it is recommended to follow this pattern:根据这篇经常引用的文章Unraveling the Mysteries of .NET Configuration ,在实现 ConfigurationSection / ConfigurationElement 时,建议遵循以下模式:

private static ConfigurationPropertyCollection s_properties;
static ExampleSection()
{
        // Predefine properties here
        // Add the properties to s_properties
}

/// Override the Properties collection and return our custom one.
protected override ConfigurationPropertyCollection Properties
{
    get { return s_properties; }
}

But it does not explain why the s_properties field would need to be static, and the properties initialized in a static constructor.但它没有解释为什么s_properties字段需要是 static,以及在 static 构造函数中初始化的属性。
After all, it's only accessed via the non-static Properties overridden property...毕竟,它只能通过非静态Properties覆盖的属性来访问......

(I have a complicated set of custom configuration management, it would greatly simplify things having the s_properties field be NOT static...) (我有一套复杂的自定义配置管理,它将大大简化s_properties字段不是 static 的事情......)

So, is there some "hidden" access directly to the static field?那么,是否有一些直接对 static 字段的“隐藏”访问? Is the Configuration*** object continually created and re-created, such that object-level fields would be lost, and as such is inefficient? Configuration*** object 是否不断创建和重新创建,从而导致对象级字段丢失,因此效率低下?
Or is it perfectly fine to store and initialize the ConfigurationPropertyCollection as non-static?或者将ConfigurationPropertyCollection存储和初始化为非静态是否完全可以?

But it does not explain why the s_properties field would need to be static,但它没有解释为什么 s_properties 字段需要是 static,

The reason s_properties is static is because you the developer only wanted one set - a singleton instance - of configuration properties for an application. s_properties static 的原因是因为开发人员只需要一组 - singleton 实例 - 应用程序的配置属性。 [ And this may be considered typical .] The ConfigurationXXX (ConfigurationManager, for example) classes are static because you should only require one per AppDomain. [这可能被认为是典型的。] ConfigurationXXX(例如,ConfigurationManager)类是 static,因为每个 AppDomain 只需要一个。

Basically, in the sample:基本上,在示例中:

private static ConfigurationPropertyCollection s_properties;
static ExampleSection()
{
        // Predefine properties here
        // Add the properties to s_properties
}

/// Override the Properties collection and return our custom one.
protected override ConfigurationPropertyCollection Properties
{
    get { return s_properties; }
}

the author is assuming you should only have a single instance of s_properties .作者假设您应该只有一个s_properties实例。

So, is there some "hidden" access directly to the static field?那么,是否有一些直接对 static 字段的“隐藏”访问?

Not sure I follow this question.不确定我是否遵循这个问题。

Is the Configuration*** object continually created and re-created, such that object-level fields would be lost, and as such is inefficient?配置*** object 是否不断创建和重新创建,从而导致对象级字段丢失,因此效率低下?

[Edit: Depends... I was considering the ConfigurationManager. [编辑:取决于...我正在考虑配置管理器。 But, say I wanted to have a local copy of my AppSettingsCollection.但是,假设我想要我的 AppSettingsCollection 的本地副本。 I would make that a static readonly field declaration.我会做一个 static 只读字段声明。 In the context of Sections, etc. Then I would still use a static field inializer, albeit a static Dictionary<string, ConfigurationPropertyCollection> Properties .在 Sections 等的上下文中,我仍然会使用 static 字段初始化程序,尽管是static Dictionary<string, ConfigurationPropertyCollection> Properties In the end, I still believe that you would only ever want a single instance of your property settings collection.最后,我仍然相信您只需要一个属性设置集合的实例。 ] ]

[Original with other [ edits ] ] [原创与其他[编辑]]
No. [ Static ] ConfigurationXXX objects are not continually created, disposed, and re-created.否。 [ Static ] ConfigurationXXX 对象不会不断创建、处置和重新创建。 Like all static objects, a public static initializer/constructor is executed a single time - the first time that object is called.像所有 static 对象一样,公共 static 初始化器/构造器被执行一次 - 第一次调用 object。 For instance, when I call the following:例如,当我调用以下命令时:

string someValue = ConfigurationManager.AppSettings["SomeKey"];  

The static ConfigurationManager class constructor is executed.执行 static ConfigurationManager class 构造函数。 Since this class is static, it will live for the lifetime of the current AppDomain in which it is executed.由于此 class 是 static,因此它将在执行它的当前 AppDomain 的生命周期内存活。

I would not recommend initializing app domain properties, configuration properties, or property collections as or within an instance class.我不建议将应用程序域属性、配置属性或属性 collections 作为 或在 实例 class 中初始化。 When your program begins execution, you should rely on a static Configuration class in which you wrap and customize interaction with ConfigurationXXX classes, members, and functions.当您的程序开始执行时,您应该依赖 static 配置 class,您可以在其中包装和自定义与 ConfigurationXXX 类、成员和函数的交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM