DLL loading into a separate AppDomain

I want to load one or more DLLs dynamically so that they start with a different protection or base path than my main application. How to load these DLLs into a separate AppDomain and create objects from them?

+32
c # appdomain
Sep 17 '08 at 23:36
source share
5 answers

More specific

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 right (no exceptions), now you 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 regular object.

Note. As far as I know, TypeIWantToLoad should inherit from MarshalByRefObject.

+34
Sep 18 '08 at 14:29
source share

If you target at 3.5, you can take advantage of the new, manageable, extensible infrastructure to handle all the heavy load for you.

+4
17 Sep '08 at 23:50
source share

You can use the AppDomain.CreateInstance method for this. You will need to call the Unwrap method of the ObjectHandle object, which is returned to get the actual object.

+2
Sep 17 '08 at 23:46
source share

Create a new Appdomain with AppDomain.Create (...). After creating the AppDomain, load the DLLs into this AppDomain.

Look at all the methods that Appdomain has using Create *. There are things like CreateInstanceAndUnwrap etc.

0
Sep 17 '08 at 23:50
source share

As already mentioned, use AppDomain.CreateDomain to create a new application domain. You can then load the assembly using the Load method or complete the assembly using the ExecuteAssembly method. You can use GetAssemblies to find out if the assembly has already been loaded. Remember also that you cannot unload an assembly after loading it. You will need to unload the domain.

0
18 sept 2018-08-08T00:
source share



All Articles