Index: /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ApplicationManager.cs
===================================================================
--- /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ApplicationManager.cs	(revision 6173)
+++ /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ApplicationManager.cs	(revision 6174)
@@ -28,11 +28,16 @@
   /// </summary>
   public static class ApplicationManager {
-    // the singleton instance is initialized to LightweightApplicationManager as long as no other application manager is registered
-    private static IApplicationManager appManager = new LightweightApplicationManager();
+    // the singleton instance is initialized to LightweightApplicationManager as long as no other application manager is registered    
+    private static IApplicationManager appManager;
+
     /// <summary>
     /// Gets the application manager singleton.
     /// </summary>
     public static IApplicationManager Manager {
-      get { return appManager; }
+      get {
+        if (appManager == null)
+          appManager = new LightweightApplicationManager();
+        return appManager;
+      }
     }
 
@@ -42,5 +47,5 @@
     /// <param name="manager"></param>
     internal static void RegisterApplicationManager(IApplicationManager manager) {
-      if (!(appManager is LightweightApplicationManager)) throw new InvalidOperationException("The application manager has already been set.");
+      if (appManager != null && !(appManager is LightweightApplicationManager)) throw new InvalidOperationException("The application manager has already been set.");
       else {
         appManager = manager;
Index: /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/DefaultApplicationManager.cs
===================================================================
--- /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/DefaultApplicationManager.cs	(revision 6173)
+++ /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/DefaultApplicationManager.cs	(revision 6174)
@@ -21,54 +21,15 @@
 
 using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
-using HeuristicLab.PluginInfrastructure.Manager;
 
 namespace HeuristicLab.PluginInfrastructure {
 
-  /// <summary>
-  /// The DefaultApplicationManager provides properties to retrieve the list of available plugins and applications.
-  /// It also provides methods for type discovery and instantiation for types declared in plugins.
+  /// <summary>  
   /// The DefaultApplicationManager is registered as ApplicationManager.Manager singleton for each HL application
   /// started via the plugin infrastructure.
   /// </summary>
-  internal sealed class DefaultApplicationManager : MarshalByRefObject, IApplicationManager {
-    /// <summary>
-    /// Fired when a plugin is loaded.
-    /// </summary>
-    internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded;
-    /// <summary>
-    /// Fired when a plugin is unloaded (when the application terminates).
-    /// </summary>
-    internal event EventHandler<PluginInfrastructureEventArgs> PluginUnloaded;
-
-    // cache for the AssemblyResolveEvent 
-    // which must be handled when assemblies are loaded dynamically after the application start
-    private Dictionary<string, Assembly> loadedAssemblies;
-
-    private List<IPlugin> loadedPlugins;
-
-    private List<PluginDescription> plugins;
-    /// <summary>
-    /// Gets all plugins.
-    /// </summary>
-    public IEnumerable<IPluginDescription> Plugins {
-      get { return plugins.Cast<IPluginDescription>(); }
-    }
-
-    private List<ApplicationDescription> applications;
-    /// <summary>
-    /// Gets all installed applications.
-    /// </summary>
-    public IEnumerable<IApplicationDescription> Applications {
-      get { return applications.Cast<IApplicationDescription>(); }
-    }
+  internal sealed class DefaultApplicationManager : SandboxApplicationManager {
 
     internal DefaultApplicationManager()
       : base() {
-      loadedAssemblies = new Dictionary<string, Assembly>();
-      loadedPlugins = new List<IPlugin>();
       AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
         if (loadedAssemblies.ContainsKey(args.Name)) {
@@ -79,244 +40,5 @@
     }
 
-    /// <summary>
-    /// Prepares the application domain for the execution of an HL application.
-    /// Pre-loads all <paramref name="plugins"/>.
-    /// </summary>
-    /// <param name="apps">Enumerable of available HL applications.</param>
-    /// <param name="plugins">Enumerable of plugins that should be pre-loaded.</param>
-    internal void PrepareApplicationDomain(IEnumerable<ApplicationDescription> apps, IEnumerable<PluginDescription> plugins) {
-      this.plugins = new List<PluginDescription>(plugins);
-      this.applications = new List<ApplicationDescription>(apps);
-      ApplicationManager.RegisterApplicationManager(this);
-      LoadPlugins(plugins);
-    }
-
-    /// <summary>
-    /// Loads the <paramref name="plugins"/> into this application domain.
-    /// </summary>
-    /// <param name="plugins">Enumerable of plugins that should be loaded.</param>
-    private void LoadPlugins(IEnumerable<PluginDescription> plugins) {
-      // load all loadable plugins (all dependencies available) into the execution context
-      foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
-        foreach (string fileName in desc.AssemblyLocations) {
-          // load assembly reflection only first to get the full assembly name
-          var reflectionOnlyAssembly = Assembly.ReflectionOnlyLoadFrom(fileName);
-          // load the assembly into execution context using full assembly name
-          var asm = Assembly.Load(reflectionOnlyAssembly.FullName);
-          RegisterLoadedAssembly(asm);
-          // instantiate and load all plugins in this assembly
-          foreach (var plugin in GetInstances<IPlugin>(asm)) {
-            plugin.OnLoad();
-            loadedPlugins.Add(plugin);
-          }
-        }
-        OnPluginLoaded(new PluginInfrastructureEventArgs(desc));
-        desc.Load();
-      }
-    }
-
-    /// <summary>
-    /// Runs the application declared in <paramref name="appInfo"/>.
-    /// This is a synchronous call. When the application is terminated all plugins are unloaded.
-    /// </summary>
-    /// <param name="appInfo">Description of the application to run</param>
-    internal void Run(ApplicationDescription appInfo) {
-      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
-      try {
-        runnablePlugin.Run();
-      }
-      finally {
-        // unload plugins in reverse order
-        foreach (var plugin in loadedPlugins.Reverse<IPlugin>()) {
-          plugin.OnUnload();
-        }
-        foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
-          desc.Unload();
-          OnPluginUnloaded(new PluginInfrastructureEventArgs(desc));
-        }
-      }
-    }
-
-    /// <summary>
-    /// Loads raw assemblies dynamically from a byte array
-    /// </summary>
-    /// <param name="assemblies">bytearray of all raw assemblies that should be loaded</param>
-    internal void LoadAssemblies(IEnumerable<byte[]> assemblies) {
-      foreach (byte[] asm in assemblies) {
-        Assembly loadedAsm = Assembly.Load(asm);
-        RegisterLoadedAssembly(loadedAsm);
-      }
-    }
-
-    // register assembly in the assembly cache for the AssemblyResolveEvent
-    private void RegisterLoadedAssembly(Assembly asm) {
-      if (loadedAssemblies.ContainsKey(asm.FullName) || loadedAssemblies.ContainsKey(asm.GetName().Name)) {
-        throw new ArgumentException("An assembly with the name " + asm.GetName().Name + " has been registered already.", "asm");
-      }
-      loadedAssemblies.Add(asm.FullName, asm);
-      loadedAssemblies.Add(asm.GetName().Name, asm); // add short name
-    }
-
-    /// <summary>
-    /// Creates an instance of all types that are subtypes or the same type of the specified type and declared in <paramref name="plugin"/>
-    /// </summary>
-    /// <typeparam name="T">Most general type.</typeparam>
-    /// <returns>Enumerable of the created instances.</returns>
-    internal static IEnumerable<T> GetInstances<T>(IPluginDescription plugin) where T : class {
-      List<T> instances = new List<T>();
-      foreach (Type t in GetTypes(typeof(T), plugin, true)) {
-        T instance = null;
-        try { instance = (T)Activator.CreateInstance(t); }
-        catch { }
-        if (instance != null) instances.Add(instance);
-      }
-      return instances;
-    }
-    /// <summary>
-    /// Creates an instance of all types declared in assembly <paramref name="asm"/> that are subtypes or the same type of the specified <typeparamref name="type"/>. 
-    /// </summary>
-    /// <typeparam name="T">Most general type.</typeparam>
-    /// <param name="asm">Declaring assembly.</param>
-    /// <returns>Enumerable of the created instances.</returns>
-    private static IEnumerable<T> GetInstances<T>(Assembly asm) where T : class {
-      List<T> instances = new List<T>();
-      foreach (Type t in GetTypes(typeof(T), asm, true)) {
-        T instance = null;
-        try { instance = (T)Activator.CreateInstance(t); }
-        catch { }
-        if (instance != null) instances.Add(instance);
-      }
-      return instances;
-    }
-    /// <summary>
-    /// Creates an instance of all types that are subtypes or the same type of the specified type
-    /// </summary>
-    /// <typeparam name="T">Most general type.</typeparam>
-    /// <returns>Enumerable of the created instances.</returns>
-    internal static IEnumerable<T> GetInstances<T>() where T : class {
-      return from i in GetInstances(typeof(T))
-             select (T)i;
-    }
-
-    /// <summary>
-    /// Creates an instance of all types that are subtypes or the same type of the specified type
-    /// </summary>
-    /// <param name="type">Most general type.</param>
-    /// <returns>Enumerable of the created instances.</returns>
-    internal static IEnumerable<object> GetInstances(Type type) {
-      List<object> instances = new List<object>();
-      foreach (Type t in GetTypes(type, true)) {
-        object instance = null;
-        try { instance = Activator.CreateInstance(t); }
-        catch { }
-        if (instance != null) instances.Add(instance);
-      }
-      return instances;
-    }
-
-    /// <summary>
-    /// Finds all types that are subtypes or equal to the specified type.
-    /// </summary>
-    /// <param name="type">Most general type for which to find matching types.</param>
-    /// <param name="onlyInstantiable">Return only types that are instantiable 
-    /// (interfaces, abstract classes... are not returned)</param>
-    /// <returns>Enumerable of the discovered types.</returns>
-    internal static IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable) {
-      return from asm in AppDomain.CurrentDomain.GetAssemblies()
-             from t in GetTypes(type, asm, onlyInstantiable)
-             select t;
-    }
-
-    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool assignableToAllTypes) {
-      IEnumerable<Type> result = GetTypes(types.First(), onlyInstantiable);
-      foreach (Type type in types.Skip(1)) {
-        IEnumerable<Type> discoveredTypes = GetTypes(type, onlyInstantiable);
-        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
-        else result = result.Union(discoveredTypes);
-      }
-      return result;
-    }
-
-    /// <summary>
-    /// Finds all types that are subtypes or equal to the specified type if they are part of the given
-    /// <paramref name="pluginDescription"/>.
-    /// </summary>
-    /// <param name="type">Most general type for which to find matching types.</param>
-    /// <param name="pluginDescription">The plugin the subtypes must be part of.</param>
-    /// <param name="onlyInstantiable">Return only types that are instantiable 
-    /// (interfaces, abstract classes... are not returned)</param>
-    /// <returns>Enumerable of the discovered types.</returns>
-    internal static IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription, bool onlyInstantiable) {
-      PluginDescription pluginDesc = (PluginDescription)pluginDescription;
-      return from asm in AppDomain.CurrentDomain.GetAssemblies()
-             where !IsDynamicAssembly(asm)
-             where pluginDesc.AssemblyLocations.Any(location => location.Equals(Path.GetFullPath(asm.Location), StringComparison.CurrentCultureIgnoreCase))
-             from t in GetTypes(type, asm, onlyInstantiable)
-             select t;
-    }
-
-    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription pluginDescription, bool onlyInstantiable, bool assignableToAllTypes) {
-      IEnumerable<Type> result = GetTypes(types.First(), pluginDescription, onlyInstantiable);
-      foreach (Type type in types.Skip(1)) {
-        IEnumerable<Type> discoveredTypes = GetTypes(type, pluginDescription, onlyInstantiable);
-        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
-        else result = result.Union(discoveredTypes);
-      }
-      return result;
-    }
-
-    private static bool IsDynamicAssembly(Assembly asm) {
-      return (asm is System.Reflection.Emit.AssemblyBuilder) || string.IsNullOrEmpty(asm.Location);
-    }
-
-    /// <summary>
-    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
-    /// </summary>
-    /// <param name="type">Most general type we want to find.</param>
-    /// <param name="assembly">Assembly that should be searched for types.</param>
-    /// <param name="onlyInstantiable">Return only types that are instantiable 
-    /// (interfaces, abstract classes...  are not returned)</param>
-    /// <returns>Enumerable of the discovered types.</returns>
-    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable) {
-      return from t in assembly.GetTypes()
-             where CheckTypeCompatibility(type, t)
-             where onlyInstantiable == false ||
-                (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
-             where !IsNonDiscoverableType(t)
-             select BuildType(t, type);
-    }
-
-    private static bool IsNonDiscoverableType(Type t) {
-      return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
-    }
-
-    private static bool CheckTypeCompatibility(Type type, Type other) {
-      if (type.IsAssignableFrom(other))
-        return true;
-      if (type.IsGenericType && other.IsGenericType) {
-        try {
-          if (type.IsAssignableFrom(other.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments())))
-            return true;
-        }
-        catch (Exception) { }
-      }
-      return false;
-    }
-    private static Type BuildType(Type type, Type protoType) {
-      if (type.IsGenericType && protoType.IsGenericType)
-        return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
-      else
-        return type;
-    }
-
-    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
-      if (PluginLoaded != null) PluginLoaded(this, e);
-    }
-
-    private void OnPluginUnloaded(PluginInfrastructureEventArgs e) {
-      if (PluginUnloaded != null) PluginUnloaded(this, e);
-    }
-
-    // infinite lease time
+    /// infinite lease time
     /// <summary>
     /// Initializes the life time service with infinite lease time.
@@ -326,43 +48,4 @@
       return null;
     }
-
-    #region IApplicationManager Members
-
-    IEnumerable<T> IApplicationManager.GetInstances<T>() {
-      return GetInstances<T>();
-    }
-
-    IEnumerable<object> IApplicationManager.GetInstances(Type type) {
-      return GetInstances(type);
-    }
-
-    IEnumerable<Type> IApplicationManager.GetTypes(Type type, bool onlyInstantiable) {
-      return GetTypes(type, onlyInstantiable);
-    }
-    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool assignableToAllTypes) {
-      return GetTypes(types, onlyInstantiable, assignableToAllTypes);
-    }
-
-    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable) {
-      return GetTypes(type, plugin, onlyInstantiable);
-    }
-    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable, bool assignableToAllTypes) {
-      return GetTypes(types, plugin, onlyInstantiable, assignableToAllTypes);
-    }
-
-
-    /// <summary>
-    /// Finds the plugin that declares the <paramref name="type">type</paramref>.
-    /// </summary>
-    /// <param name="type">The type of interest.</param>
-    /// <returns>The description of the plugin that declares the given type or null if the type has not been declared by a known plugin.</returns>
-    public IPluginDescription GetDeclaringPlugin(Type type) {
-      if (type == null) throw new ArgumentNullException("type");
-      foreach (PluginDescription info in Plugins) {
-        if (info.AssemblyLocations.Contains(Path.GetFullPath(type.Assembly.Location))) return info;
-      }
-      return null;
-    }
-    #endregion
   }
 }
Index: /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/HeuristicLab.PluginInfrastructure-3.3.csproj
===================================================================
--- /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/HeuristicLab.PluginInfrastructure-3.3.csproj	(revision 6173)
+++ /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/HeuristicLab.PluginInfrastructure-3.3.csproj	(revision 6174)
@@ -220,4 +220,5 @@
     <Compile Include="BaseClasses\ApplicationBase.cs" />
     <Compile Include="BaseClasses\PluginBase.cs" />
+    <Compile Include="SandboxApplicationManager.cs" />
     <Compile Include="DefaultApplicationManager.cs" />
     <Compile Include="ErrorHandling\ErrorDialog.cs">
Index: /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs
===================================================================
--- /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs	(revision 6174)
+++ /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs	(revision 6174)
@@ -0,0 +1,353 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using HeuristicLab.PluginInfrastructure.Manager;
+
+namespace HeuristicLab.PluginInfrastructure {
+
+  /// <summary>
+  /// The SandboxApplicationManager provides properties to retrieve the list of available plugins and applications.
+  /// It also provides methods for type discovery and instantiation for types declared in plugins.
+  /// The SandboxApplicationManager is used in sandboxed Application Domains where permissions are restricted and
+  /// only partially-trusted code can be executed. 
+  /// </summary>
+  internal class SandboxApplicationManager : MarshalByRefObject, IApplicationManager {
+    /// <summary>
+    /// Fired when a plugin is loaded.
+    /// </summary>
+    internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded;
+    /// <summary>
+    /// Fired when a plugin is unloaded (when the application terminates).
+    /// </summary>
+    internal event EventHandler<PluginInfrastructureEventArgs> PluginUnloaded;
+
+    // cache for the AssemblyResolveEvent 
+    // which must be handled when assemblies are loaded dynamically after the application start
+    protected internal Dictionary<string, Assembly> loadedAssemblies;
+
+    private List<IPlugin> loadedPlugins;
+
+    private List<PluginDescription> plugins;
+    /// <summary>
+    /// Gets all plugins.
+    /// </summary>
+    public IEnumerable<IPluginDescription> Plugins {
+      get { return plugins.Cast<IPluginDescription>(); }
+    }
+
+    private List<ApplicationDescription> applications;
+    /// <summary>
+    /// Gets all installed applications.
+    /// </summary>
+    public IEnumerable<IApplicationDescription> Applications {
+      get { return applications.Cast<IApplicationDescription>(); }
+    }
+
+    internal SandboxApplicationManager()
+      : base() {
+      loadedAssemblies = new Dictionary<string, Assembly>();
+      loadedPlugins = new List<IPlugin>();
+    }
+
+    /// <summary>
+    /// Prepares the application domain for the execution of an HL application.
+    /// Pre-loads all <paramref name="plugins"/>.
+    /// </summary>
+    /// <param name="apps">Enumerable of available HL applications.</param>
+    /// <param name="plugins">Enumerable of plugins that should be pre-loaded.</param>  
+    internal void PrepareApplicationDomain(IEnumerable<ApplicationDescription> apps, IEnumerable<PluginDescription> plugins) {
+      this.plugins = new List<PluginDescription>(plugins);
+      this.applications = new List<ApplicationDescription>(apps);
+      ApplicationManager.RegisterApplicationManager(this);
+      LoadPlugins(plugins);
+    }
+
+    /// <summary>
+    /// Loads the <paramref name="plugins"/> into this application domain.
+    /// </summary>
+    /// <param name="plugins">Enumerable of plugins that should be loaded.</param>
+    private void LoadPlugins(IEnumerable<PluginDescription> plugins) {
+      // load all loadable plugins (all dependencies available) into the execution context
+      foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
+        foreach (string fileName in desc.AssemblyLocations) {
+          // load assembly reflection only first to get the full assembly name
+          var reflectionOnlyAssembly = Assembly.ReflectionOnlyLoadFrom(fileName);
+          // load the assembly into execution context using full assembly name
+          var asm = Assembly.Load(reflectionOnlyAssembly.FullName);
+          RegisterLoadedAssembly(asm);
+          // instantiate and load all plugins in this assembly
+          foreach (var plugin in GetInstances<IPlugin>(asm)) {
+            plugin.OnLoad();
+            loadedPlugins.Add(plugin);
+          }
+        }
+        OnPluginLoaded(new PluginInfrastructureEventArgs(desc));
+        desc.Load();
+      }
+    }
+
+    /// <summary>
+    /// Runs the application declared in <paramref name="appInfo"/>.
+    /// This is a synchronous call. When the application is terminated all plugins are unloaded.
+    /// </summary>
+    /// <param name="appInfo">Description of the application to run</param>
+    internal void Run(ApplicationDescription appInfo) {
+      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
+      try {
+        runnablePlugin.Run();
+      }
+      finally {
+        // unload plugins in reverse order
+        foreach (var plugin in loadedPlugins.Reverse<IPlugin>()) {
+          plugin.OnUnload();
+        }
+        foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
+          desc.Unload();
+          OnPluginUnloaded(new PluginInfrastructureEventArgs(desc));
+        }
+      }
+    }
+
+    /// <summary>
+    /// Loads raw assemblies dynamically from a byte array
+    /// </summary>
+    /// <param name="assemblies">bytearray of all raw assemblies that should be loaded</param>
+    internal void LoadAssemblies(IEnumerable<byte[]> assemblies) {
+      foreach (byte[] asm in assemblies) {
+        Assembly loadedAsm = Assembly.Load(asm);
+        RegisterLoadedAssembly(loadedAsm);
+      }
+    }
+
+    // register assembly in the assembly cache for the AssemblyResolveEvent
+    private void RegisterLoadedAssembly(Assembly asm) {
+      if (loadedAssemblies.ContainsKey(asm.FullName) || loadedAssemblies.ContainsKey(asm.GetName().Name)) {
+        throw new ArgumentException("An assembly with the name " + asm.GetName().Name + " has been registered already.", "asm");
+      }
+      loadedAssemblies.Add(asm.FullName, asm);
+      loadedAssemblies.Add(asm.GetName().Name, asm); // add short name
+    }
+
+    /// <summary>
+    /// Creates an instance of all types that are subtypes or the same type of the specified type and declared in <paramref name="plugin"/>
+    /// </summary>
+    /// <typeparam name="T">Most general type.</typeparam>
+    /// <returns>Enumerable of the created instances.</returns>
+    internal static IEnumerable<T> GetInstances<T>(IPluginDescription plugin) where T : class {
+      List<T> instances = new List<T>();
+      foreach (Type t in GetTypes(typeof(T), plugin, true)) {
+        T instance = null;
+        try { instance = (T)Activator.CreateInstance(t); }
+        catch { }
+        if (instance != null) instances.Add(instance);
+      }
+      return instances;
+    }
+    /// <summary>
+    /// Creates an instance of all types declared in assembly <paramref name="asm"/> that are subtypes or the same type of the specified <typeparamref name="type"/>. 
+    /// </summary>
+    /// <typeparam name="T">Most general type.</typeparam>
+    /// <param name="asm">Declaring assembly.</param>
+    /// <returns>Enumerable of the created instances.</returns>
+    private static IEnumerable<T> GetInstances<T>(Assembly asm) where T : class {
+      List<T> instances = new List<T>();
+      foreach (Type t in GetTypes(typeof(T), asm, true)) {
+        T instance = null;
+        try { instance = (T)Activator.CreateInstance(t); }
+        catch { }
+        if (instance != null) instances.Add(instance);
+      }
+      return instances;
+    }
+    /// <summary>
+    /// Creates an instance of all types that are subtypes or the same type of the specified type
+    /// </summary>
+    /// <typeparam name="T">Most general type.</typeparam>
+    /// <returns>Enumerable of the created instances.</returns>
+    internal static IEnumerable<T> GetInstances<T>() where T : class {
+      return from i in GetInstances(typeof(T))
+             select (T)i;
+    }
+
+    /// <summary>
+    /// Creates an instance of all types that are subtypes or the same type of the specified type
+    /// </summary>
+    /// <param name="type">Most general type.</param>
+    /// <returns>Enumerable of the created instances.</returns>
+    internal static IEnumerable<object> GetInstances(Type type) {
+      List<object> instances = new List<object>();
+      foreach (Type t in GetTypes(type, true)) {
+        object instance = null;
+        try { instance = Activator.CreateInstance(t); }
+        catch { }
+        if (instance != null) instances.Add(instance);
+      }
+      return instances;
+    }
+
+    /// <summary>
+    /// Finds all types that are subtypes or equal to the specified type.
+    /// </summary>
+    /// <param name="type">Most general type for which to find matching types.</param>
+    /// <param name="onlyInstantiable">Return only types that are instantiable 
+    /// (interfaces, abstract classes... are not returned)</param>
+    /// <returns>Enumerable of the discovered types.</returns>
+    internal static IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable) {
+      return from asm in AppDomain.CurrentDomain.GetAssemblies()
+             from t in GetTypes(type, asm, onlyInstantiable)
+             select t;
+    }
+
+    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool assignableToAllTypes) {
+      IEnumerable<Type> result = GetTypes(types.First(), onlyInstantiable);
+      foreach (Type type in types.Skip(1)) {
+        IEnumerable<Type> discoveredTypes = GetTypes(type, onlyInstantiable);
+        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
+        else result = result.Union(discoveredTypes);
+      }
+      return result;
+    }
+
+    /// <summary>
+    /// Finds all types that are subtypes or equal to the specified type if they are part of the given
+    /// <paramref name="pluginDescription"/>.
+    /// </summary>
+    /// <param name="type">Most general type for which to find matching types.</param>
+    /// <param name="pluginDescription">The plugin the subtypes must be part of.</param>
+    /// <param name="onlyInstantiable">Return only types that are instantiable 
+    /// (interfaces, abstract classes... are not returned)</param>
+    /// <returns>Enumerable of the discovered types.</returns>
+    internal static IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription, bool onlyInstantiable) {
+      PluginDescription pluginDesc = (PluginDescription)pluginDescription;
+      return from asm in AppDomain.CurrentDomain.GetAssemblies()
+             where !IsDynamicAssembly(asm)
+             where pluginDesc.AssemblyLocations.Any(location => location.Equals(Path.GetFullPath(asm.Location), StringComparison.CurrentCultureIgnoreCase))
+             from t in GetTypes(type, asm, onlyInstantiable)
+             select t;
+    }
+
+    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription pluginDescription, bool onlyInstantiable, bool assignableToAllTypes) {
+      IEnumerable<Type> result = GetTypes(types.First(), pluginDescription, onlyInstantiable);
+      foreach (Type type in types.Skip(1)) {
+        IEnumerable<Type> discoveredTypes = GetTypes(type, pluginDescription, onlyInstantiable);
+        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
+        else result = result.Union(discoveredTypes);
+      }
+      return result;
+    }
+
+    private static bool IsDynamicAssembly(Assembly asm) {
+      return (asm is System.Reflection.Emit.AssemblyBuilder) || string.IsNullOrEmpty(asm.Location);
+    }
+
+    /// <summary>
+    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
+    /// </summary>
+    /// <param name="type">Most general type we want to find.</param>
+    /// <param name="assembly">Assembly that should be searched for types.</param>
+    /// <param name="onlyInstantiable">Return only types that are instantiable 
+    /// (interfaces, abstract classes...  are not returned)</param>
+    /// <returns>Enumerable of the discovered types.</returns>
+    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable) {
+      return from t in assembly.GetTypes()
+             where CheckTypeCompatibility(type, t)
+             where onlyInstantiable == false ||
+                (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
+             where !IsNonDiscoverableType(t)
+             select BuildType(t, type);
+    }
+
+
+    private static bool IsNonDiscoverableType(Type t) {
+      return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
+    }
+
+    private static bool CheckTypeCompatibility(Type type, Type other) {
+      if (type.IsAssignableFrom(other))
+        return true;
+      if (type.IsGenericType && other.IsGenericType) {
+        try {
+          if (type.IsAssignableFrom(other.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments())))
+            return true;
+        }
+        catch (Exception) { }
+      }
+      return false;
+    }
+    private static Type BuildType(Type type, Type protoType) {
+      if (type.IsGenericType && protoType.IsGenericType)
+        return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
+      else
+        return type;
+    }
+
+    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
+      if (PluginLoaded != null) PluginLoaded(this, e);
+    }
+
+    private void OnPluginUnloaded(PluginInfrastructureEventArgs e) {
+      if (PluginUnloaded != null) PluginUnloaded(this, e);
+    }
+
+    #region IApplicationManager Members
+
+    IEnumerable<T> IApplicationManager.GetInstances<T>() {
+      return GetInstances<T>();
+    }
+
+    IEnumerable<object> IApplicationManager.GetInstances(Type type) {
+      return GetInstances(type);
+    }
+
+    IEnumerable<Type> IApplicationManager.GetTypes(Type type, bool onlyInstantiable) {
+      return GetTypes(type, onlyInstantiable);
+    }
+    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool assignableToAllTypes) {
+      return GetTypes(types, onlyInstantiable, assignableToAllTypes);
+    }
+
+    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable) {
+      return GetTypes(type, plugin, onlyInstantiable);
+    }
+    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable, bool assignableToAllTypes) {
+      return GetTypes(types, plugin, onlyInstantiable, assignableToAllTypes);
+    }
+
+    /// <summary>
+    /// Finds the plugin that declares the <paramref name="type">type</paramref>.
+    /// </summary>
+    /// <param name="type">The type of interest.</param>
+    /// <returns>The description of the plugin that declares the given type or null if the type has not been declared by a known plugin.</returns>
+    public IPluginDescription GetDeclaringPlugin(Type type) {
+      if (type == null) throw new ArgumentNullException("type");
+      foreach (PluginDescription info in Plugins) {
+        if (info.AssemblyLocations.Contains(Path.GetFullPath(type.Assembly.Location))) return info;
+      }
+      return null;
+    }
+    #endregion
+  }
+}
Index: /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Sandboxing/SandboxManager.cs
===================================================================
--- /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Sandboxing/SandboxManager.cs	(revision 6173)
+++ /trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Sandboxing/SandboxManager.cs	(revision 6174)
@@ -21,77 +21,79 @@
 
 using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
 using System.Reflection;
 using System.Security;
 using System.Security.Permissions;
-using System.Security.Policy;
 using HeuristicLab.PluginInfrastructure.Manager;
 
 namespace HeuristicLab.PluginInfrastructure.Sandboxing {
-  public class SandboxManager {
+  public static class SandboxManager {
 
-    // static class
-    private SandboxManager() { }
+    /// <summary>
+    /// Creates an privileged sandbox, meaning that the executed code is fully trusted and permissions are not restricted.
+    /// This method is a fall back for trusted users in HeuristicLab Hive. 
+    /// </summary>    
+    public static AppDomain CreateAndInitPrivilegedSandbox(string appDomainName, string applicationBase, string configFilePath) {
+      PermissionSet pSet;
+      pSet = new PermissionSet(PermissionState.Unrestricted);
 
-    private static StrongName CreateStrongName(Assembly assembly) {
-      if (assembly == null)
-        throw new ArgumentNullException("assembly");
-
-      AssemblyName assemblyName = assembly.GetName();
-      Trace.Assert(assemblyName != null, "Could not get assembly name");
-
-      // get the public key blob
-      byte[] publicKey = assemblyName.GetPublicKey();
-      if (publicKey == null || publicKey.Length == 0)
-        throw new InvalidOperationException("Assembly is not strongly named");
-
-      StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
-
-      // and create the StrongName
-      return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
-    }
-
-    #region ISandboxManager Members
-    public static AppDomain CreateAndInitSandbox(string appDomainName, string applicationBase, string configFilePath) {
-      PermissionSet pset;
-
-      #region permission set for sandbox
-      // Uncomment code for sandboxed appdomain
-      //pset = new PermissionSet(PermissionState.None);
-      //pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
-      //pset.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
-      //FileIOPermission fPerm = new FileIOPermission(PermissionState.None);
-
-      //foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
-      //  fPerm.AddPathList(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, plugin.Files.ToArray());
-      //}
-
-      //pset.AddPermission(fPerm);
-      #endregion
-
-      #region permission set of unrestricted appdomain
-      // unrestricted appdomain 
-      pset = new PermissionSet(PermissionState.Unrestricted);
-      #endregion
-
-      AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
+      AppDomainSetup setup = new AppDomainSetup();
       setup.PrivateBinPath = applicationBase;
       setup.ApplicationBase = applicationBase;
       setup.ConfigurationFile = configFilePath;
 
-      AppDomain applicationDomain = AppDomain.CreateDomain(appDomainName, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
       Type applicationManagerType = typeof(DefaultApplicationManager);
-      DefaultApplicationManager applicationManager =
-        (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null);
+      AppDomain applicationDomain = AppDomain.CreateDomain(appDomainName, null, setup, pSet, null);
+      DefaultApplicationManager applicationManager = (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null);
+
       PluginManager pm = new PluginManager(applicationBase);
       pm.DiscoverAndCheckPlugins();
-      ApplicationDescription[] apps = pm.Applications.Cast<ApplicationDescription>().ToArray();
-      PluginDescription[] plugins = pm.Plugins.Cast<PluginDescription>().ToArray();
-      applicationManager.PrepareApplicationDomain(apps, plugins);
+      applicationManager.PrepareApplicationDomain(pm.Applications, pm.Plugins);
+
       return applicationDomain;
     }
-    #endregion
+
+    /// <summary>
+    /// Creates a sandbox with restricted permissions.
+    /// Code that is executed in such an AppDomain is partially-trusted and is not allowed to call or override
+    /// methods that require full trust. 
+    /// </summary>    
+    public static AppDomain CreateAndInitSandbox(string appDomainName, string applicationBase, string configFilePath) {
+      PermissionSet pSet;
+
+      pSet = new PermissionSet(PermissionState.None);
+      pSet.AddPermission(new SecurityPermission(PermissionState.None));
+      pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
+      pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Infrastructure));
+      pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
+      pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.SerializationFormatter));
+      pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.ControlAppDomain));
+      //needed for HeuristicLab.Persistence, see DynamicMethod Constructor (String, Type, array<Type []()>[], Type, Boolean)
+      pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.ControlEvidence));
+
+      ReflectionPermission refPerm = new ReflectionPermission(PermissionState.Unrestricted);
+      pSet.AddPermission(refPerm);
+
+      FileIOPermission ioPerm = new FileIOPermission(PermissionState.None);
+      //allow path discovery for system drive, needed by HeuristicLab.Persistence: Serializer.BuildTypeCache() -> Assembly.CodeBase
+      ioPerm.AddPathList(FileIOPermissionAccess.PathDiscovery, Environment.SystemDirectory.Substring(0, 3));
+      //allow full access to the appdomain's base directory
+      ioPerm.AddPathList(FileIOPermissionAccess.AllAccess, applicationBase);
+      pSet.AddPermission(ioPerm);
+
+      AppDomainSetup setup = new AppDomainSetup();
+      setup.PrivateBinPath = applicationBase;
+      setup.ApplicationBase = applicationBase;
+      setup.ConfigurationFile = configFilePath;
+
+      Type applicationManagerType = typeof(SandboxApplicationManager);
+      AppDomain applicationDomain = AppDomain.CreateDomain(appDomainName, null, setup, pSet, null);
+      SandboxApplicationManager applicationManager = (SandboxApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null);
+
+      PluginManager pm = new PluginManager(applicationBase);
+      pm.DiscoverAndCheckPlugins();
+      applicationManager.PrepareApplicationDomain(pm.Applications, pm.Plugins);
+
+      return applicationDomain;
+    }
   }
 }
