What is a .NET application domain?

An AppDomain basically provides an isolated region in which code runs inside of a process. An easy way to think of it is almost like a lighter-weight process sitting inside of your main process. Each AppDomain exists within a process in complete isolation, which allows you to run code safely (it can be unloaded without … Read more

Is there a way to force all referenced assemblies to be loaded into the app domain?

This seemed to do the trick: var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray(); var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, “*.dll”); var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList(); toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path)))); As Jon noted, the ideal solution would need to recurse into the dependencies for each of the loaded assemblies, but in my specific scenario … Read more

How can I reliably determine the type of a variable that is declared using var at design time?

I can describe for you how we do that efficiently in the “real” C# IDE. The first thing we do is run a pass which analyzes only the “top level” stuff in the source code. We skip all the method bodies. That allows us to quickly build up a database of information about what namespace, … Read more

How to Load an Assembly to AppDomain with all references recursively?

You need to invoke CreateInstanceAndUnwrap before your proxy object will execute in the foreign application domain. class Program { static void Main(string[] args) { AppDomainSetup domaininfo = new AppDomainSetup(); domaininfo.ApplicationBase = System.Environment.CurrentDirectory; Evidence adevidence = AppDomain.CurrentDomain.Evidence; AppDomain domain = AppDomain.CreateDomain(“MyDomain”, adevidence, domaininfo); Type type = typeof(Proxy); var value = (Proxy)domain.CreateInstanceAndUnwrap( type.Assembly.FullName, type.FullName); var assembly = … Read more

How can I implement ISerializable in .NET 4+ without violating inheritance security rules?

According to the MSDN, in .NET 4.0 basically you should not use ISerializable for partially trusted code, and instead you should use ISafeSerializationData Quoting from https://learn.microsoft.com/en-us/dotnet/standard/serialization/custom-serialization Important In versions previous to .NET Framework 4.0, serialization of custom user data in a partially trusted assembly was accomplished using the GetObjectData. Starting with version 4.0, that method … Read more

What is AppDomain? [duplicate]

An AppDomain provides a layer of isolation within a process. Everything you usually think of as “per program” (static variables etc) is actually per-AppDomain. This is useful for: plugins (you can unload an AppDomain, but not an assembly within an AppDomain) security (you can run a set of code with specific trust levels) isolation (you … Read more