Matt
Matt

Reputation: 7254

load dll library and run code within during runtime

I have a C# console application and like to chose by user input a specific dll library that is loaded and executed during the runtime of the console app. Is that possible?

So, for example, I may have 2 dll libraries with the same static class and Action name as follows:

public static class CoreStrategy
{
    public static Action<List<Quote>> strategyQuoteBuffer = new Action<List<Quote>>(quoteList =>
    {
        Console.WriteLine("I am dll 1");
    });
}

public static class CoreStrategy
{
    public static Action<List<Quote>> strategyQuoteBuffer = new Action<List<Quote>>(quoteList =>
    {
        Console.WriteLine("I am dll 2");
    });
}

How can I load one of them during the runtime of my console app and invoke them, then switch one for the other one? Or are there better ways to handle this? Maybe even different ways from Dlls? Requirement is that the code of each is strictly contained within its own dll, only and the dlls cannot be referenced beforehand. If that is not doable then would you be able to suggest a way without the usage of dlls? Thanks

Upvotes: 1

Views: 4863

Answers (2)

cuongle
cuongle

Reputation: 75296

If reflection is slow, then you might need to take a look fasterflect.

http://www.codeproject.com/Articles/38840/Fasterflect-a-fast-and-simple-API-for-Reflection-i

Upvotes: 2

Chris
Chris

Reputation: 4671

You could use reflection to dynamically load an unreferenced assembly, dynamically load a class named 'CoreStrategy' from that assembly, and then dynamically search that class for a static field named strategyQuoteBuffer. Then you could use reflection to retrieve that field, and cast it to an Action<List<Quote>>.

The code to do this certainly wouldn't be pretty, but it's the only way to achieve what you want.

Upvotes: 2

Related Questions