Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading DLLs into a separate AppDomain

Tags:

c#

.net

appdomain

I want to load one or more DLLs dynamically so that they run with a different security or basepath than my main application. How do I load these DLLs into a separate AppDomain and instantiate objects from them?

like image 462
Jon Turner Avatar asked Sep 17 '08 23:09

Jon Turner


Video Answer


1 Answers

More specifically

AppDomain domain = AppDomain.CreateDomain("New domain name"); //Do other things to the domain like set the security policy  string pathToDll = @"C:\myDll.dll"; //Full path to dll you want to load Type t = typeof(TypeIWantToLoad); TypeIWantToLoad myObject = (TypeIWantToLoad)domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName); 

If all that goes properly (no exceptions thrown) you now have an instance of TypeIWantToLoad loaded into your new domain. The instance you have is actually a proxy (since the actual object is in the new domain) but you can use it just like your normal object.

Note: As far as I know TypeIWantToLoad has to inherit from MarshalByRefObject.

like image 192
Jon Turner Avatar answered Sep 22 '22 22:09

Jon Turner