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

查看:47
本文介绍了在类库的 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天全站免登陆