How to Make Application.Properties.Settings public

2019-04-18 11:34发布

问题:

I am in the process of centralizing my application settings into one location and I've chosen to use the the Settings collection in my common library to do so.

I have moved all these settings into their own file that gets pulled into my app.config using config source:

<Common.Properties.Settings configSource="config\Common.Properties.Settings.config" />

This allows me to use the "Add Link" capability of Visual Studio to override the default library settings with the imported config file within my web and test applications.

Now, I want to be able to access all of these great Settings values from within my other libraries, and have found that I can do so simply by making the generated class public:

File: Common.Properties.Settings

public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase

This lets me get access to things like Common.Properties.Settings.Default.MySetting from within my web application or unit tests. However, the problem is that whenever a new setting is added, the Setting.settings file is regenerated by Visual Studio, and flips the Settings class back to internal:

internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase

So my question is whether anyone knows of a way to override this, or perhaps suggest a macro approach or some other method to ensure that after the Settings.settings file is rebuilt, this class is set to public.

Thanks!

回答1:

I assume that you have created and modified settings form visual studio build in editor that can be accessed through project properties section settings. On the same editor there is an combobox that you need to change from internal to public it is marked as Access Modifier.