public static List <string> GetAssemblyDependencyList(string filename, string searchPath = "") { AppDomain newDomain = AppDomain.CreateDomain("Sandbox"); newDomain.Load(Assembly.GetExecutingAssembly().GetName()); _asm_factory_ factory = newDomain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().GetName().FullName, "Trinity.Utilities._asm_factory_") as _asm_factory_; var ret = factory.__get_assembly_dependency_list_with_filename__(filename, searchPath); AppDomain.Unload(newDomain); return((List <string>)ret); }
/// <summary> /// This method load an assembly into the nameless context. /// Note that unlike Assembly.Load, this method will prevent /// auto-resolving on disk. When a dependency is not already /// loaded into AssemblyCache, it will not load the new assembly. /// </summary> /// <param name="asmBytes">Target assembly binary</param> /// <param name="lockedAction">Action to execute while auto-resolving is disabled</param> /// <param name="ResolveToLoadedAssembly">If the assembly is already loaded, don't load again</param> /// <returns>An Assembly if loaded successful, otherwise null</returns> public static Assembly Load(byte[] asmBytes, Action <Assembly> lockedAction, bool ResolveToLoadedAssembly) { if (lockedAction != null) { AppDomain newDomain = AppDomain.CreateDomain("Sandbox"); newDomain.Load(Assembly.GetExecutingAssembly().GetName()); _asm_factory_ factory = newDomain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().GetName().FullName, "Trinity.Utilities._asm_factory_") as _asm_factory_; List <string> depList = factory.__get_assembly_dependency_list__(asmBytes); //Unload the domain, release the assemblies AppDomain.Unload(newDomain); List <FileStream> locked_files = LockDependencyFiles(depList); Assembly ret = null; if (!ResolveToLoadedAssembly) { ret = Assembly.Load(asmBytes); } else { newDomain = AppDomain.CreateDomain("Sandbox"); newDomain.Load(Assembly.GetExecutingAssembly().GetName()); factory = newDomain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().GetName().FullName, "Trinity.Utilities._asm_factory_") as _asm_factory_; string fullName = factory.__get_assembly_fullname__(asmBytes); AppDomain.Unload(newDomain); if (AssemblyCache.ContainsKey(fullName)) { ret = AssemblyCache[fullName]; } else { ret = Assembly.Load(asmBytes); } } lockedAction(ret); UnlockDependencyFiles(locked_files); return(ret); } else { return(Assembly.Load(asmBytes)); } }
internal static string LookupAssemblyLocation(string fullName, string path) { AppDomain newDomain = AppDomain.CreateDomain("Sandbox"); newDomain.Load(Assembly.GetExecutingAssembly().GetName()); string fullPath = ""; _asm_factory_ factory = newDomain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().GetName().FullName, "Trinity.Utilities._asm_factory_") as _asm_factory_; fullPath = factory.__lookup_assembly_location__(fullName, path); AppDomain.Unload(newDomain); return(fullPath); }
/// <summary> /// Scan through all dll/exe files in current working directory, /// load them up and check whether they match the given full asm name. /// If one matches the given full name, return an reflection-only assembly of that file. /// These interfaces might be slow since it creates a sandbox for the assembly loading. /// </summary> /// <param name="fullname">The full name of the desired assembly</param> /// <param name="searchPath">The path to search</param> /// <param name="fullPath">The full path of the located assembly.</param> /// <returns>Null if file not found. An assembly if found.</returns> internal static Assembly LoadReflectionOnly(string fullname, string searchPath, out string fullPath) { AppDomain newDomain = AppDomain.CreateDomain("Sandbox"); newDomain.Load(Assembly.GetExecutingAssembly().GetName()); fullPath = ""; _asm_factory_ factory = newDomain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().GetName().FullName, "Trinity.Utilities._asm_factory_") as _asm_factory_; var ret = factory.__load_reflection_only__(fullname, searchPath, out fullPath); AppDomain.Unload(newDomain); return((Assembly)ret); }