使用类库中的多个 .settings 类,如何进行?

发布于 2024-08-06 05:15:54 字数 2316 浏览 2 评论 0原文

有两个项目:一个是类库(.dll 程序集),另一个是使用类库的 GUI (.exe)。

在类库中,我使用所有“应用程序”设置添加了两个 .settings 文件(它自动将 app.config 文件添加到项目中)。当我编译项目时,我有效地在输出目录中获取了 MyLib.dll.config 文件。

现在,对于 GUI 项目,我引用类库并手动将 MyLib.dll.config 复制到 bin\debug(和发布)文件夹。

我试图从 GUI 应用程序获取 MyLib.dll.config 设置,但到目前为止它不起作用。我尝试将 .settings 类设置为公共,但没有成功。

我已阅读有关 OpenMappedExeConfiguration() 方法的信息,但我似乎无法在返回的配置中找到设置。此外,我如何强制 .settings 类使用 OpenMappedExeConfiguration 返回的配置?

注意: 我不想手动将 MyLib.dll.config 中的条目添加到 GUI 应用程序的 app.config 中,因为这些条目根本不属于那里。


附加说明:

我拥有的 .config 文件是:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="TestAssembly.ItemClass" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <section name="TestAssembly.Entity" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <TestAssembly.ItemClass>
            <setting name="Job" serializeAs="String">
                <value>TrussPlant.Job</value>
            </setting>
            <setting name="Family" serializeAs="String">
                <value>TrussPlant.Family</value>
            </setting>
        </TestAssembly.ItemClass>
        <TestAssembly.Entity>
            <setting name="TrussClassifier" serializeAs="String">
                <value>TrussPlant.Classifier</value>
            </setting>
            <setting name="TrussComposer" serializeAs="String">
                <value>TrussPlant.Composer</value>
            </setting>
        </TestAssembly.Entity>
    </applicationSettings>
</configuration>

请注意,有一个SectionGroup,其中包含两个部分。

Having two projects: one is a class library (a .dll assembly) and the other is a GUI (.exe) using the class library.

In the class library, I've added two .settings files (which automatically adds the app.config file to the project) using all "application" settings. When I compile the project I effectively get the MyLib.dll.config file in the output directory.

Now, for the GUI project, I reference the class library and manually copy the MyLib.dll.config to the bin\debug (and release) folder.

I am trying to get the MyLib.dll.config settings from the GUI application but so far it is not working. I've tried having the .settings classes set to public but I was unsuccessful.

I've read about the OpenMappedExeConfiguration() method but I can't seem to find the settings in the returned config. Furthermore, how would I force the .settings classes to use the config returned by OpenMappedExeConfiguration?

Note: I do not want to manually add the entries from MyLib.dll.config to the app.config of the GUI application since those entries do not belong there at all.


Additional notes:

The .config file that I have is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="TestAssembly.ItemClass" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <section name="TestAssembly.Entity" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <TestAssembly.ItemClass>
            <setting name="Job" serializeAs="String">
                <value>TrussPlant.Job</value>
            </setting>
            <setting name="Family" serializeAs="String">
                <value>TrussPlant.Family</value>
            </setting>
        </TestAssembly.ItemClass>
        <TestAssembly.Entity>
            <setting name="TrussClassifier" serializeAs="String">
                <value>TrussPlant.Classifier</value>
            </setting>
            <setting name="TrussComposer" serializeAs="String">
                <value>TrussPlant.Composer</value>
            </setting>
        </TestAssembly.Entity>
    </applicationSettings>
</configuration>

Note that there is a SectionGroup which contains the two sections.

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

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

发布评论

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

评论(2

爱人如己 2024-08-13 05:15:54

假设您的 DLL 项目在 .\Properties\Settings.Designer.cs 中有类似的内容:

namespace _SampleApplication.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
...

然后您可以使用以下代码将其添加到 DLL 项目中的另一个文件中:

namespace _SampleApplication.Properties {
    partial class Settings
    {
        static System.Configuration.Configuration _config = null;

        private System.Configuration.Configuration DllConfig
        {
            get
            {
                return _config != null ? _config : 
                    _config = System.Configuration.ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
            }
        }   

        public override object this[string propertyName]
        {
            get { return DllConfig.AppSettings.Settings[propertyName].Value; }
            set { DllConfig.AppSettings.Settings[propertyName].Value = value as string; }
        }
    }
}

更多的是“黑客”解决方案,我不知道如何做正确的事,但它会起作用。

Assuming your DLL project has something like this in .\Properties\Settings.Designer.cs:

namespace _SampleApplication.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
...

Then you can add this to another file in your DLL project with the following code:

namespace _SampleApplication.Properties {
    partial class Settings
    {
        static System.Configuration.Configuration _config = null;

        private System.Configuration.Configuration DllConfig
        {
            get
            {
                return _config != null ? _config : 
                    _config = System.Configuration.ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
            }
        }   

        public override object this[string propertyName]
        {
            get { return DllConfig.AppSettings.Settings[propertyName].Value; }
            set { DllConfig.AppSettings.Settings[propertyName].Value = value as string; }
        }
    }
}

More of a 'hack' solution, I don't know how to do right, but it will work.

你与清晨阳光 2024-08-13 05:15:54

一般来说,配置设置应该属于应用程序,而不是组件。

也就是说,组件应该能够读取自己的设置,因此向组件添加一个读取设置的方法,然后从应用程序调用该方法。

In general, config settings should belong to the application, not to the component.

That said, the component should be able to read its own settings, so add a method to the component that reads the settings and then call that method from the application.

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