如何从引用的类库中读取 Application.Setting

发布于 2024-09-17 19:57:31 字数 469 浏览 5 评论 0原文

设想: 我有 2 个项目,MainApplication(编译为 exe)和 ClassLibrary1。 现在 MainApplication 引用或加载 ClassLibrary1,但 ClassLibrary1 不知道 MainApplication。

但我想使用 MainApplication 中定义的设置(Dot.Net 2.0 的 Properties.Settings NOT appSettings)。

你如何实现这一目标?

例子

System.Configuration.ConfigurationSettings.AppSettings.Get("SettingName");

我似乎有很多使用这与我的情况无关的

,因为 appSettings 是老派的,我正在使用较新的 Properties.Settings 机制。感谢您的帮助:)

Scenario:
I have 2 Projects, MainApplication (which compiles to an exe) and ClassLibrary1.
Now MainApplication references or loads ClassLibrary1, but ClassLibrary1 has no idea about MainApplication.

But I want to use Settings (Dot.Net 2.0's Properties.Settings NOT appSettings) that are defined in MainApplication.

How do you achieve this?

I have seem PLENTY examples that use

System.Configuration.ConfigurationSettings.AppSettings.Get("SettingName");

This is NOT relevant to my situation as appSettings is old school and I am using the newer Properties.Settings mechanisms.

Your help is appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攀登最高峰 2024-09-24 19:57:31

我在代码中做了一些调查。
我可以得到这样的设置,但它真的很脏:

((ClientSettingsSection)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["applicationSettings"].Sections["MainApplication.Properties.Settings"]).Settings.Get("Tester").Value.ValueXml.InnerText;

也许有人可以提供更优雅的解决方案

I have done some investigating in code.
I can get the setting like this but it is really dirty:

((ClientSettingsSection)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["applicationSettings"].Sections["MainApplication.Properties.Settings"]).Settings.Get("Tester").Value.ValueXml.InnerText;

Maybe someone can provide a more elegant solition

一身骄傲 2024-09-24 19:57:31

由于 Settings 类是在 Main 项目中定义的,因此您无法直接从类项目访问它,因为这会创建循环依赖项。
您要做的就是为类库提供一个委托,它可以调用该委托来从主项目中动态检索设置。创建一个在类库中存储委托的类,并将该委托设置为 Main 项目中定义的方法。该方法将封装检索设置所需的指令。

在我看来,appSettings 并不是老派,它只是一种表示配置参数的方式,这些参数不是用户特定的或可定制的。

Since the Settings class is defined on the Main project, you cannot directly access it from the class project because you would create a circular dependency.
What you would do is provide the Class library with a delegate it can call to dynamically retrieve the settings from the main project. Create a class that stores a delegate in the Class library and set this delegate to a method defined in the Main project. This methods would encapsulate the instructions needed to retrieve a setting.

And in my opinion, appSettings is not old school, it is just a way of representing configuration parameters that are not specific or customizable by a user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文