Home of the AlmostImplementedException

Load a DLL at runtime – with hash-check

If you work with very dynamic code or external libraries or need to update your own libraries regularly, you sometimes need to load DLL’s at runtime.
With C# that’s very easy. We just have to use the Assembly-class from System.Reflection and call the static methods LoadFrom or LoadFile.

You can get a lot of information from the assembly class, but today we’ll just get a class we want, create an instance and call 2 methods.
First we’ll have to create the class we want to load:

After you compiled a DLL with this class, we can load and use it.
The following code is working with .Net 4 or higher because since then you can use “dynamic”.
Add it to a C# console program to see the resulting output.

If you want to do the same with an older version of .Net, you have to use reflection. This is a bit more complicated, but also no problem.
The following code does exactly the same as the above:

A problem with dynamic loading of DLLs is the potential risk of a manipulation. To prevent this you can check the hash of the dll.
But first you need a function to calculate it. I use MD5 for this example, but you can use what ever you want.

Now you need to check the hash while loading the DLL. .Net provides a special method for this, LoadFrom. You can assign the hash and the used algorithm as parameter.
You’ll get a FileLoadException if the hash doesn’t match.

You can do a lot of stuff on top of that like zipping or encrypt your DLLs, but that’s up to you :-).
There are other things you can do with the Assembly-class too, you play with it.

And one final word of warning: If your dynamic code is wrong the compiler won’t tell you. You’ll just get an exception at runtime!

Share :

One thought on “Load a DLL at runtime – with hash-check

Leave a Reply

Your email address will not be published. Required fields are marked *