如何从引用的类库中读取 Application.Setting
设想: 我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在代码中做了一些调查。
我可以得到这样的设置,但它真的很脏:
也许有人可以提供更优雅的解决方案
I have done some investigating in code.
I can get the setting like this but it is really dirty:
Maybe someone can provide a more elegant solition
由于 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.