/// <summary>
/// Loads the assembly into the specified <see cref="AppDomain" />.
/// </summary>
/// <param name="appDomain">The app domain.</param>
/// <param name="assemblyName">The assembly name.</param>
/// <param name="includeReferencedAssemblies">if set to <c>true</c>, referenced assemblies will be included as well.</param>
/// <exception cref="ArgumentNullException">The <paramref name="appDomain"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="assemblyName"/> is <c>null</c>.</exception>
public static void LoadAssemblyIntoAppDomain(this AppDomain appDomain, AssemblyName assemblyName, bool includeReferencedAssemblies = true)
{
Argument.IsNotNull("appDomain", appDomain);
Argument.IsNotNull("assemblyName", assemblyName);
Log.Debug("Preloading assembly '{0}'", assemblyName);
try
{
if (appDomain.GetAssemblies().Any(assembly => AssemblyName.ReferenceMatchesDefinition(assemblyName, assembly.GetName())))
{
Log.Debug("Assembly already loaded into the AppDomain");
return;
}
var loadedAssembly = appDomain.Load(assemblyName);
if (includeReferencedAssemblies)
{
Log.Debug("Loading referenced assemblies of assembly '{0}'", assemblyName);
var referencedAssemblies = loadedAssembly.GetReferencedAssemblies();
foreach (var referencedAssembly in referencedAssemblies)
{
LoadAssemblyIntoAppDomain(appDomain, referencedAssembly, false);
}
}
// Convenience call
TypeCache.InitializeTypes(false, loadedAssembly);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to load assembly '{0}'", assemblyName);
}
}