mercredi 14 novembre 2018

Loading DLLs dynamically from the local disk

While the programming takes place and you refer to your files in NuGet packages programming with dependencies is not a so big deal, but I made an extension app to an existing application which relies basically on the same dll files as the main program.

Now I don't want to actually place the extension in our main application directory because it is more handy to have it versatile and have the user or admin copy it a to a place they like to.

In pseudocode the code while programming looks like this:

using Newtonsoft.Json;
...
public void xyz()
{
    JsonSerializer ObjectSerializer = new JsonSerializer();
    ... Do Stuff ...
}

I can be perfectly sure to have the fitting dll file in a local directory on the concerning machines, so there is registry key where I can read the actual path out of.

Without dynamically wanting to load the dll the code runs fine, but I hope that something like this would be possible (in the Program.cs - without any using directives to 3rd party dlls!!! - namespace might be loaded anyway):

private static void Main(string[] args)
{
    string ProgramPath = FindProgramPath();
    System.Reflection.Assembly.LoadFrom(ProgramPath + @"System.Net.Http.Formatting.dll");
    System.Reflection.Assembly.LoadFrom(ProgramPath + @"Newtonsoft.Json.dll");
    ... Do Stuff
}

Should the logic be wrapped around in another file which will be loaded in the current context, maybe as a resource to be sure that the rest of the needed files are loaded accordingly? Or does somebody else know a way to be independent of the folder location without bloating the actual exe file up?





Aucun commentaire:

Enregistrer un commentaire