在类库的App.config中使用ConfigurationManager.GetSection [英] Using ConfigurationManager.GetSection in a Class Library's App.config

查看:1347
本文介绍了在类库的App.config中使用ConfigurationManager.GetSection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写其具有在其的app.config并且将最终由少量的其他的.NET应用程序调用的设置的类库。为了得到我使用ConfigurationManager.GetSection从中设置,像这样的:

I am writing a class library which has settings in its app.config and which will ultimately be called by a small number of other .NET applications. In order to get settings from it I'm using ConfigurationManager.GetSection such as this:

MyConfiguration process = (MyConfiguration)ConfigurationManager.GetSection("MyGroup/processes");



我发现,虽然调用应用程序必须有它自己的项目中相同的app.config为了使这项工作,否则类库将抛出一个的NullReferenceException 。我只是想知道,如果这是正常的行为,或者有什么办法,以确保只有类库都需要有可用的app.config

I've discovered though that the calling application has to have the same app.config inside it's own project in order for this to work, otherwise the class library will throw a NullReferenceException. I'm just wondering if this is normal behavior or if there's any way to ensure that only the class library needs to have app.config available?

感谢:?)

推荐答案

您类库总是会尝试从引用它的主要应用在app.config阅读。它不会将您的类库配置文件都没有。

Your class library will always attempt to read from the app.config of the main application that references it. It will not use your class library config file at all.

这篇关于在类库的App.config中使用ConfigurationManager.GetSection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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