Thursday, April 28, 2011

How do I access the ApplicationSettings section from another assembly?

I have a dll that is referenced by the parent (executing) assembly. In the ApplicationSettings section of the app.config for the parent assembly I have some settings that can be accessed in the normal intellisense manner (e.g. Properties.Settings.Default.SMTPServer).

How do I access these settings from the referenced dll? I obviously can't use intellisense as they are not in the same assembly!

From stackoverflow
  • Are you accessing that value from both assemblies?

    The dll can have it's own config file and app settings. This starts out as a dll with a config file named after it, but the settings can be moved to the main application as well.

    Another approach that I have used is assign the value to an IoC and then read the value out of the Ioc (Ioc == Inversion of Control library). You can do the same thing by assigning the value to a singleton class.

    Calanus : I am not accessing the value from both assemblies. The reason that I want the executing assembly to hold the setting is because the referenced dll is used/compiled by many different applications but the settings will be different for each application - hence the settings cannot be defined in the referenced dll.
  • Have you tried saving a .settings file in your DLL project and then using its [] operator to access the property by name?

    For example, let's say your DLL have a MySettings.settings file which has nothing in particular in it:

    MySettings.Default["SomeSetting"];

    (Note, I haven't actually tried this, but after a quick think it seems like it ought to work)

    I think a better solution would be to take Chris' advice and use a singleton or IOC mechanism, that way you wouldn't have to sacrifice type safety.

0 comments:

Post a Comment