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?
-
Possible duplicate of [Simplest way to make cross-appdomain call?](http://stackoverflow.com/questions/6242573/simplest-way-to-make-cross-appdomain-call) – user626528 Feb 14 '17 at 18:23
-
The context of the referenced possible duplicate is different. In the referenced question the goal is to call code in an AppDomain not created by or under control of the developer. This question is specifically about loading code in an AppDomain that the developer controls so that they can modify the characteristics of the AppDomain. Also the referenced question also does not give a complete code example of how to dynamically load a DLL at runtime, which is a key component of this question. – Jon Turner Feb 15 '17 at 16:41
5 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.

- 2,932
- 3
- 22
- 20
If you're targeting 3.5, you can take advantage of the new managed extensibility framework to handle all the heavy lifting for you.
-
@dboar Well, it will be part of the framework in 4.0, but AFAIK you can use the preview in 3.5 (the download page says they have "merged the .net 3.5 and silverlight binaries"). – Feb 10 '10 at 00:54
-
oh, cool...I never went past the initial codeplex page cuz they specifically said .Net 4.0...good to kno...thx – IAbstract Feb 10 '10 at 01:21
You can use the AppDomain.CreateInstance method to do this. You'll need to call the Unwrap method of the ObjectHandle that is returned to get at the actual object.

- 30,088
- 6
- 78
- 89
-
What if the object whose instance i'd like to create is defined in the assembly i'm trying to load? I cannot know what is the name of this object without actually loading the assembly first... – lysergic-acid Jun 06 '11 at 08:33
-
@liortal Does the accepted answer to this question do what you need to do? – Andy Jun 06 '11 at 12:58
-
We'd like to load a set of DLLs into another AppDomain, however the types defined in these DLLs is not known to us. – lysergic-acid Jun 06 '11 at 13:13
-
@liortal You should be able to use AppDomain.Load() to load a DLL into that AppDomain. That returns an Assembly instance, which you can use to query any types in that assembly. – Andy Jun 07 '11 at 10:58
-
Just as a heads up, Andy's suggestion to use AppDomain.Load() may work, but it will also load the requested assembly in the current domain, which seems to defeat the purpose of using a different domain. See http://msdn.microsoft.com/en-us/library/36az8x58(v=vs.110).aspx – dhochee Nov 20 '14 at 01:18
Create a new Appdomain with AppDomain.Create( ... ). After creating the AppDomain load the DLLs into that AppDomain.
Look into all the methods that Appdomain has with Create*. There are certain things like CreateInstanceAndUnwrap, etc.

- 2,975
- 1
- 19
- 24
As previously stated, use AppDomain.CreateDomain to create a new app domain. You can then load an assembly into it using the Load method, or even execute an assembly using the ExecuteAssembly method. You can use GetAssemblies to see if an assembly has already been loaded. Be aware too that you cannot unload an assembly once it's loaded. You will need to unload the domain.

- 2,220
- 1
- 15
- 14