1

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

Matt
  • 7,004
  • 11
  • 71
  • 117
  • See http://stackoverflow.com/questions/1087794/c-sharp-load-a-dll-file-and-access-methods-from-class-within – Liron Aug 01 '12 at 15:30
  • Also, see https://stackoverflow.com/questions/72700743/c-sharp-load-dll-at-runtime/72845914#72845914 – Caleb Liu Jul 09 '22 at 12:29

2 Answers2

2

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.

Chris
  • 4,661
  • 1
  • 23
  • 25
  • reflection sounds very slow, am I incorrect? I need something that really rocks, speed wise. – Matt Aug 01 '12 at 15:44
  • My goal is to be able to load a "strategy", which serves up code structured as methods. I can ensure that each strategy provides the exact same method signature, just that the code inside the method is different. The loaded methods would then be available to be called. I have seen application that accomplish exactly that and I doubt it was built on reflection. Just wondering what your take on that is. – Matt Aug 01 '12 at 15:47
  • How slow is slow? More importantly, how slow is *too* slow? Reflection isn't renowned for being super-fast, true, but that's only a problem if it's *too slow*. You won't know that until you try it. Secondly, you're trying to load code from a dll that isn't referenced, and reflection is the only way to do that. In short: try it, if it works it works, if it's too slow you need to change your entire design. – Chris Aug 01 '12 at 15:52
  • As for that alternative design (if you do decide that reflection is too slow - and again, you need to try it before you know that): drop the whole idea of having separate assemblies. Also, in this case I'd suggest dropping the idea of the classes/methods being static. Create an ICoreStrategy interface with the methods/properties you need. Write two (or more) classes inheriting from that interface, and a factory class that picks which one of those classes to use (based on, I assume, a config file) and returns it as an ICoreStrategy for the rest of your code to use. – Chris Aug 01 '12 at 15:56
  • thanks I will follow your advice and attempt to use reflection first. Any pointers to sample code how to load the assemblies at runtime and access through reflection? Thanks a lot. – Matt Aug 01 '12 at 17:21
2

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

cuongle
  • 74,024
  • 28
  • 151
  • 206
  • Interesting. The performance numbers look very impressive. I will probably start out with standard .Net reflection and if performance suffers will take a look at your referenced API. Thanks – Matt Aug 01 '12 at 17:29