访问其他项目的app.config属性? [英] Accessing another projects app.config properties?
问题描述
我有我的解决方案中的两个项目,在这个例子中,我会打电话给他们的项目A和B。
项目B引用A.能项目B访问的应用程序。项目A的配置属性?
我想A的的app.config中访问应用程序密钥字符串。
字符串tfsUri = ConfigurationManager.AppSettings [TfsUri];
这通常不是一个好主意,因为你介绍硬项目之间的依赖关系。所以,如果你可以复制粘贴配置值,这将使你的项目自包含的(然而,这引入了配置值的重复)。
您也可以自动执行此,这样,当你建立一个项目的配置依赖关系自动解决。
说完,说这个,还有其他的选择,并且在每一种情况下,你可能更愿意使用别的东西。您的其他选项有:
- 使用配置改造,像的 SlowCheetah
- 从一个项目添加整个配置文件到另一个的为纽带
- 配置价值注入到类(而不是从配置读它)
- 阅读运行时的其它项目的配置文件,通过使用如 ConfigurationManager.OpenExeConfiguration 一件事>
- 还检查了如何选择不同的app.config几构建配置
I have two projects within my solution, for this example I will call them project A and B.
Project B references A. Can Project B access the app.config properties of project A?
I wish to access an app key string within the app.config of A.
string tfsUri = ConfigurationManager.AppSettings["TfsUri"];
That's generally not a good idea, as you introduce hard dependencies between the projects. So if you can copy-paste config value, that will make your projects self-contained (however, this introduces duplication of config value).
You can also automate this, so that when you build a project the configuration dependency is automatically resolved.
Having-said this, there are other options, and in each case you may prefer to use something else. Your other options are:
- Use configuration transformation, like SlowCheetah
- Add whole config file from one project to another "as a link"
- Inject configuration value to your class (instead of reading it from config)
- Read other project's config file in runtime, by using things like ConfigurationManager.OpenExeConfiguration
- Also check out How to select different app.config for several build configurations
这篇关于访问其他项目的app.config属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!