访问另一个项目 app.config 属性?
我的解决方案中有两个项目,在本示例中,我将它们称为项目 A 和 B.
I have two projects within my solution, for this example I will call them project A and B.
项目 B 引用 A.项目 B 可以访问项目 A 的 app.config 属性吗?
Project B references A. Can Project B access the app.config properties of project A?
我希望访问 A 的 app.config 中的应用密钥字符串.
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:
- 使用配置转换,例如 SlowCheetah
- 将整个配置文件从一个项目添加到另一个作为链接"
- 将配置值注入您的类(而不是从配置中读取)
- 在运行时读取其他项目的配置文件,使用诸如 ConfigurationManager.OpenExeConfiguration
- 另请查看如何为多个构建配置选择不同的 app.config