我可以在不知道配置节名称的情况下以编程方式获取配置节吗
在 .NET 配置中,您可以编写自己的配置部分,并且在如何构造它(作为 XML)方面有很大的自由度。因此,在大多数情况下,我们这样做:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="MySection" type="MySectionType, MySectionAssembly" ... />
</configSections>
<MySection>
...
</MySection>
</configuration>
然后,要访问代码中的部分,我们执行以下操作:
MySection mySection = (MySection) ConfigurationManager.GetSection("MySection");
上面的示例是我见过的在代码中获取自定义部分的唯一方法。
想象一下,现在我们有一个库或框架,它定义了 MySection
类及其内部结构的类。我们希望向第三方开发人员提供这些类和结构,他们将在其应用程序中定义配置文件。同时,我们的框架需要以编程方式获取该部分才能正常运行。
显然,我们框架的最终用户有绝对的自由将 MySection
命名为其他任何内容。因此上面的代码将无法加载该部分。有没有一个好的方法来克服这个问题?我想避免决定强迫图书馆的使用者仅按照我们想要的方式命名他们的部分。
编辑: 该场景的另一个复杂之处是将节放入节组中:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="MyGroup">
<section name="MySection" type="MySectionType, MySectionAssembly" ... />
</sectionGroup>
</configSections>
<MyGroup>
<MySection>
...
</MySection>
</MyGroup>
</configuration>
是否也可以处理这种情况?
提前致谢。
In .NET configuration, you can write your own configuration section and have quite a lot of freedom in how to structure it (as XML). So in most cases we do this:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="MySection" type="MySectionType, MySectionAssembly" ... />
</configSections>
<MySection>
...
</MySection>
</configuration>
Then, to access our section in code, we do the following:
MySection mySection = (MySection) ConfigurationManager.GetSection("MySection");
The example above is the only way I've seen to have a custom section obtained in code.
Imagine now that we have a library or framework, that defines the MySection
classes and the classes for the structure inside it. We want to provide these classes and structure to a third party developers that will define in their applications the configuration files. At the same time, our framework will need to obtain that section programmatically in order to function properly.
Clearly, the end user of our framework has the absolute freedom to name MySection
to anything else. Therefore the code above will fail to load the section. Is there a nice way to overcome this issue? Deciding to force the consumers of the library to name their section only in the way we desire is something I would like to avoid doing.
EDIT:
An additional complication to the scenario is putting the section in a section group:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="MyGroup">
<section name="MySection" type="MySectionType, MySectionAssembly" ... />
</sectionGroup>
</configSections>
<MyGroup>
<MySection>
...
</MySection>
</MyGroup>
</configuration>
Would it be possible to handle this situation too?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该不是问题。请参阅:
http://msdn.microsoft.com/en-我们/library/system.configuration.configurationsectiongroup.aspx
Should not be a problem. See this:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationsectiongroup.aspx