开发者

In C#, can one modify an application's configuration at runtime, and if so, how?

开发者 https://www.devze.com 2023-02-10 09:56 出处:网络
I\'m writing an assembly, using C#, which will be accessed via it\'s COM interface. Fo开发者_C百科r this reason it does not have a specific exe, and therefore no [appname].exe.config.

I'm writing an assembly, using C#, which will be accessed via it's COM interface.

Fo开发者_C百科r this reason it does not have a specific exe, and therefore no [appname].exe.config.

The assembly requires some application settings. Is there a way to modify the application settings at runtime, so that it doesn't require [appname].exe.config?


There is a way, but it is not all that easy.

The config mechanism in .NET is scoped by AppDomain. If you spawn your own child AppDomain you can specify where its config should be obtained from. All types/objects which you then load and execute in the child AppDomain can pick up their configuration from your specified custom config source.

Tools like NUnit use this technique to allow you to specify the config for the code being tested without having to put it into NUnit.exe.config.

0

精彩评论

暂无评论...
验证码 换一张
取 消