Mono 中多个 C# 库的应用程序设置 (Linux)
我有一个项目正在移植到 Linux 下的 Mono 上工作。该项目包括多个 C# 库,它们有自己的用户范围的应用程序设置(通过 VS2008 自动生成)。最终,该项目将在 Windows 中的 .NET 和 Ubuntu 10.04 中的 Mono 上运行。
在 Windows 中操作时,应用程序设置存储在单个 user.config 文件中,每个库都有单独的配置部分:
<configuration>
<configSections>
<sectionGroup name="userSettings" type="UserSettingsGroup ...>
<section name="LibFoo.Properties.Settings" type="ClientSettingsSection ... />
<section name="LibBar.Properties.Settings" type="ClientSettingsSection ... />
</sectionGroup>
</configSections>
<userSettings>
<LibFoo.Properties.Settings>
<setting name="strFoo" serializeAs="String">
<value>An example string value.</value>
</setting>
</LibFoo.Properties.Settings>
<LibBar.Properties.Settings>
<setting name="strBar" serializeAs="String">
<value>An example string value.</value>
</setting>
</LibBar.Properties.Settings>
</userSettings>
</configuration>
在 Ubuntu 中,会创建单个 user.config,但是,我发现只为第一次调用的库创建一个部分Properties.Settings.Save(),并且所有库都使用相同的部分:
<configuration>
<configSections>
<sectionGroup name="userSettings" type="UserSettingsGroup ... >
<section name="LibFoo.Properties.Settings" type="ClientSettingsSection ... />
</sectionGroup>
</configSections>
<userSettings>
<LibFoo.Properties.Settings>
<setting name="strFoo" serializeAs="String">
<value>An example string value.</value>
</setting>
<setting name="strBar" serializeAs="String">
<value>An example string value.</value>
</setting>
</LibFoo.Properties.Settings>
</userSettings>
</configuration>
当库之间的设置具有相同名称时,这变得特别麻烦。
这是 Mono 中的一个错误,还是我需要对 Mono 进行一些更改才能为我的库设置生成单独的部分?
- Ubuntu v10.04
- 单运行时 v2.10.8
- Visual Studio 2008 Professional
I have a project being ported to work on Linux under Mono. The project includes multiple C# libraries, which have their own user-scoped, Application Settings (generated automatically via VS2008). In the end, the project will run on .NET in Windows and Mono in Ubuntu 10.04.
When operating in Windows, Application Settings are stored in a single user.config file with separate Configuration Sections for each library:
<configuration>
<configSections>
<sectionGroup name="userSettings" type="UserSettingsGroup ...>
<section name="LibFoo.Properties.Settings" type="ClientSettingsSection ... />
<section name="LibBar.Properties.Settings" type="ClientSettingsSection ... />
</sectionGroup>
</configSections>
<userSettings>
<LibFoo.Properties.Settings>
<setting name="strFoo" serializeAs="String">
<value>An example string value.</value>
</setting>
</LibFoo.Properties.Settings>
<LibBar.Properties.Settings>
<setting name="strBar" serializeAs="String">
<value>An example string value.</value>
</setting>
</LibBar.Properties.Settings>
</userSettings>
</configuration>
In Ubuntu, a single user.config is created, however, I find that only one section is created for whichever library first calls Properties.Settings.Save(), and all libraries use that same section:
<configuration>
<configSections>
<sectionGroup name="userSettings" type="UserSettingsGroup ... >
<section name="LibFoo.Properties.Settings" type="ClientSettingsSection ... />
</sectionGroup>
</configSections>
<userSettings>
<LibFoo.Properties.Settings>
<setting name="strFoo" serializeAs="String">
<value>An example string value.</value>
</setting>
<setting name="strBar" serializeAs="String">
<value>An example string value.</value>
</setting>
</LibFoo.Properties.Settings>
</userSettings>
</configuration>
This becomes particularly troubling when settings among the libraries have the same name.
Is this a bug in Mono, or do I need to change something for Mono to generate separate sections for my library's settings?
- Ubuntu v10.04
- mono-runtime v2.10.8
- Visual Studio 2008 Professional
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了避免相同的配置名称问题,您可以尝试将配置设置放在每个 DLL 的配置文件中。您的配置文件需要具有名称“MyDLL.dll.config”(假设您的 DLL 名为“MyDLL.dll”)并且与 DLL 位于同一文件夹中。
(从未在单声道下测试过)
要写入dll的配置文件:
您可以创建一个扩展方法“Save”,并在该方法中使用反射来检索程序集名称(调用程序集,而不是当前程序集),然后使用SaveAs保存配置。这样你就有了一种保存 dll 配置文件的方法。
To avoid the same config name problem, you can try to put the configuration settings in the config file for each DLL. Your config file needs to have the name "MyDLL.dll.config" (assuming your DLL is named "MyDLL.dll") and be in the same folder as the DLL.
(never tested under mono)
To write in dll's config files:
You can make an Extension method "Save", and in this method use reflection to retrieve the Assembly name (Calling assembly, not current assembly), then use SaveAs to save the configuration. This way you have a method to save in dll config files.