I have made a .NET class library in C# that initializes some logging, sent to an external tool. The library is altogether separate from any application, but in order to initialize it, I need to make at least one method call to it.
Is there a way for me to put something into app.config that will auto-load that dll, and call something in it? I can change the contents to suit whatever, I don't need to support any own class name or method name or whatnot.
Note, I need this to be done without any changes to the application in question, save for changing the app.config file.
Is this开发者_Python百科 possible? If so, what should I be looking at?
It would probably be considered a hack, but if you put something that inherits ConfigurationSection in your dll, and add that configuration section to your app.config, this would allow you to execute the code in the configuration section's constructor and thus do pretty much what you want to. It will of course only be invoked once, at application start-up but if I understood you correct that would be enough.
This had me puzzled for a while on how to do it. I originally thought it could be achieved by creating a custom WebProxy that would configure the logging, and loading it into the main application using the defaultProxy configuration element. This however suffer from the same problem as the other configuration suggestions in that the code is only run when required (in this case when a HTTP request is used) - thus requiring a change to the original application.
I have achieved it though by reversing the approach. Instead of trying to get the original application to configure the logging, you could write a stub of an application that configures the logging and then launches the original application.
As an example:
I have a WinForms application called Forms.exe
whose entry point is defined as:
[STAThread]
internal static void Main()
{
Application.Run(new MainForm());
}
In my stub application (which I have as a console application), I configure the logging and then load and run Forms.exe
:
internal static void Main()
{
ConfigureLogging()
Assembly app = Assembly.LoadFrom(@".\Forms.exe");
app.EntryPoint.Invoke(null, null);
}
This uses reflection to load the other application into the one that configures the logging.
Caveats:
- the other application has to be a .Net application in order to load it this way
- you might need to use Reflector to inspect the other application to work out the correct arguments to pass to the entry point (ie. if it takes
string[] args
, you might need to pass in an emptystring[]
instead of anull
as the arguments) - the original application 's console window will hang around while the other application runs (this is probably not a problem, but if it is you could try hiding it using FreeConsole)
Snoop does this using some C++ voodoo. Fortunately the source is available - look in the project named "ManagedInjector"
Yes, you can use reflection to load the content of assembly
精彩评论