3

I'm trying to load an assembly and create classes from it dynamically. This is the code:

assembly = Assembly.Load(assemblyName);
var type = assembly.GetType(assemblyName + ".MessageProcessor");
var processor = (IMessageProcessor)Activator.CreateInstance(type);
processor.Process();

The problem is that the assembly name belongs to an assembly that is not referenced in the project. I deploy it manually to the execution folder.

But I receive this error:

Could not load file or assembly 'AssemblyNameToLoad, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

I can't manually add all of these files to deps.json. Is there a way to make .NET Core load assembly in spite of not being present in deps.json file?

1 Answers1

2

LoadFrom should solve your problem as it loads a "physical" file.

I should add that you still have to communicate with it so some knowledge about it is prefereable, unless you want to go all reflection, so both loader and loaded should reference the same contract project.

LosManos
  • 7,195
  • 6
  • 56
  • 107
  • 1
    How about it's dependencies? A DLL might have some dependencies and those still throw error. – mohammad rostami siahgeli Jan 07 '19 at 20:17
  • 1
    No one said it was easy. You might run into name clashes, [order of loading](https://stackoverflow.com/questions/45588101/why-do-my-assemblies-need-to-be-loaded-in-a-specific-order), what happens if you load twice? unloading, and more. Easy? no. Fun? yes! – LosManos Jan 08 '19 at 10:09