Accessing a DLLs Configuration Settings from a separate assembly

If you separate a lot of the UI for a WinForms project into separate DLLs you can run into the problem of managing application settings. Several workarounds have been proposed. C#2.0 introduced the friend keyword which provides an elegant workaround for this problem.

If your main exe is in assembly A then you typically want to save application settings before the application exits. However, if the application settings for the UI are associated with a separate dll then, by default, assembly A can't access these application settings via the automatically generated Properties.Settings class (it's marked internal).

The workaround is to make assembly A a "friend" of the other assembly (e.g., your windows controls library). This can be done in AssemblyInfo.cs of the other assembly with the following line:

[assembly: InternalsVisibleTo("assemblyA.exe")]

Then inside of assembly A you can access the strongly-typed Settings class:


No comments:

Post a Comment