/// <summary> /// Adds a new Assembly to the assembly collection /// </summary> /// <param name="assembly">The assembly to be added</param> /// <exception cref="NotSupportedException"> /// <paramref name="assembly"/> is a dynamic assembly /// </exception> public static void AddAssembly(Assembly assembly) { if (assembly.IsDynamic) { throw new NotSupportedException("Dynamic assemblies are not supported"); } if (_assemblies.Any(x => x.ManifestModule.Name.GetHashCode() == assembly.ManifestModule.Name.GetHashCode() && x.ManifestModule.Name == assembly.ManifestModule.Name)) { return; } _assemblies.Add(assembly); object cauldronObject = null; foreach (var item in assembly.DefinedTypes) { if (item.FullName != null && item.FullName.GetHashCode() == CauldronClassName.GetHashCode() && item.FullName == CauldronClassName) { #if NETFX_CORE || NETCORE cauldronObject = Activator.CreateInstance(item.AsType()); #else cauldronObject = Activator.CreateInstance(item); #endif _cauldron.Add(cauldronObject); } } AssemblyAndResourceNamesInfo.AddRange(assembly.GetManifestResourceNames().Select(x => new AssemblyResource(assembly, x))); LoadedAssemblyChanged?.Invoke(null, new AssemblyAddedEventArgs(assembly, cauldronObject)); }
/// <summary> /// Adds a collection of new Assembly to the assembly collection. /// </summary> /// <param name="assemblies">The collection of assemblies to be added.</param> public static void AddAssemblies(IEnumerable <Assembly> assemblies) { var newLoadedAssembliesList = new List <Tuple <Assembly, MethodInfo> >(); foreach (var item in assemblies) { newLoadedAssembliesList.Add(AddAssembly(item, false)); } LoadedAssemblyChanged?.Invoke(null, new AssemblyAddedEventArgs(newLoadedAssembliesList.Select(x => x.Item1).ToArray(), newLoadedAssembliesList.Select(x => x.Item2).ToArray())); }
/// <summary> /// Loads the contents of all assemblies that matches the specified filter. /// </summary> /// <param name="directory">The directory where the assemblies are located</param> /// <param name="filter"> /// The search string to match against the names of files in <paramref name="directory"/>. /// This parameter can contain a combination of valid literal path and wildcard (* and ?) /// characters, but doesn't support regular expressions. /// </param> /// <exception cref="DirectoryNotFoundException">The path is invalid or does not exist.</exception> /// <exception cref="FileLoadException">A file that was found could not be loaded</exception> public static void LoadAssemblies(DirectoryInfo directory, string filter = "*.dll") { if (!directory.Exists) { throw new DirectoryNotFoundException("Unable to find directory: " + directory.FullName); } var newLoadedAssembliesList = new List <Tuple <Assembly, MethodInfo> >(); var files = directory.GetFiles(filter); for (int i = 0; i < files.Length; i++) { newLoadedAssembliesList.Add(AddAssembly(Assembly.LoadFile(files[i].FullName), false)); } LoadedAssemblyChanged?.Invoke(null, new AssemblyAddedEventArgs(newLoadedAssembliesList.Select(x => x.Item1).ToArray(), newLoadedAssembliesList.Select(x => x.Item2).ToArray())); }
private static Tuple <Assembly, MethodInfo> AddAssembly(Assembly assembly, bool triggerEvent) { try { if (assembly.IsDynamic) { Debug.WriteLine("Dynamic assemblies are not supported"); return(null); } if (_assemblies.Contains(assembly)) { return(null); } _assemblies.Add(assembly); var manifestResourceNames = assembly.GetManifestResourceNames(); for (int i = 0; i < manifestResourceNames.Length; i++) { var resource = new AssemblyResource(assembly, manifestResourceNames[i]); _assemblyAndResourceNamesInfo.Add(resource); } var cauldronHelper = assembly.GetType("CauldronInterceptionHelper")?.GetMethod("GetComponents", BindingFlags.Public | BindingFlags.Static); if (triggerEvent && cauldronHelper != null) { LoadedAssemblyChanged?.Invoke(null, new AssemblyAddedEventArgs(assembly, cauldronHelper)); } return(new Tuple <Assembly, MethodInfo>(assembly, cauldronHelper)); } catch (Exception e) { Debug.WriteLine("Error while loading an assembly" + e.Message); } return(null); }