Andrew Simpson Andrew Simpson - 1 month ago 17
C# Question

how to load a dll into ram and have it accessible by another dll that has been loaded into ram

I have a WPF desktop app.

My idea is this:

My installed application starts up and makes a connection to my server.

The client app then loads my Views and DLL Logic/Resources into RAM.

This is how i do that:

var uri = "http://127.0.0.1/Dll/MyControls.Dll";
var data = new System.Net.WebClient().DownloadData(uri);
var ViewAssembly = System.Reflection.Assembly.Load(data);

foreach (Type type in ViewAssembly.GetExportedTypes())
{
if (type.FullName.IndexOf(".Plugin") > 0)
{
dynamic obj = Activator.CreateInstance(type);
UserControl wnd = obj.GetControl();
MainContent.Children.Add(wnd);
break;
}
}

uri = "http://127.0.0.1/Dll/Biz.Dll";
data = new System.Net.WebClient().DownloadData(uri);
var DLLAssembly = System.Reflection.Assembly.Load(data);
Type myType = DLLAssembly.GetType("Biz.Logic");


Now in my 'original' application my Views will invoke logic from my Biz.Dll via its reference.

But in this scenario the DLL is loaded into RAM and not the Users hard drive.
So, is it possible for my View (which has been loaded into RAM) to invoke a method in Biz.Dll (also loaded into RAM) whilst not writing the DLL onto the Users hard drive?

The reason for my approach is that i wish to have a small physical footprint on any users PC and control the access to the code. So, whenever the users starts the app up it always gets the latest version and i can control the registration of the app.

I know I could move all my main logic to my server but I am not trying to protect my code and also i want the app to still function even if the user cannot get a good (ie broken - temporarily) internet connection. I 'cache' all my server updates from my client and it updates when there IS a connection.

I could possible re-engineer my code to make late binding calls but i do not know how that would work with my binding markups in my xaml..

Any approaches/ideas would be welcome.

thanks

Answer

If you load both assemblies before calling Activator.CreateInstace your code will work. It doesn't make a difference for runtime whether your dll is loaded from harddrive or byte array through Assembly.Load, so if you know all dependencies beforehand, you can preload all of them.

In case you don't know what assemblies will be needed at runtime you can always subscribe to AppDomain.AssemblyResolve event and download only requested dll's.

Comments