Exemplo n.º 1
0
        public static Type[] GetTypesImplementing <T>(string assemblyName = "")
        {
            IEnumerable <Assembly> assemblies = null;

            if (assemblyName.IsNotEmpty() &&
                LoadedAssemblies.Count(a => assemblyName.IsNotEmpty() && a.GetName().Name == assemblyName) > 0)
            {
                assemblies = LoadedAssemblies.Where(a => a.FullName.StartsWith(assemblyName));
            }
            else if (assemblyName.IsEmpty())
            {
                assemblies = LoadedAssemblies;
            }
            else
            {
                throw new InvalidOperationException($"The assembly {assemblyName} is not loaded.");
            }

            IEnumerable <Type> types =
                from type in assemblies.SelectMany(a => a.GetTypes())
                where typeof(T).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract
                select type;

            return(types.ToArray());
        }
Exemplo n.º 2
0
 /// <summary>
 /// Get a list of all embedded assemblies
 /// </summary>
 /// <returns>IEnumerable with a containing the names of the resource and of the assemblie</returns>
 public IEnumerable <string> EmbeddedAssemblyNames(IEnumerable <Assembly> assembliesToCheck = null)
 {
     foreach (var loadedAssembly in assembliesToCheck ?? LoadedAssemblies.Where(pair => !AssembliesToIgnore.IsMatch(pair.Key)).Select(pair => pair.Value).ToList())
     {
         string[] resources;
         try
         {
             resources = Resources.GetCachedManifestResourceNames(loadedAssembly);
         }
         catch (Exception ex)
         {
             Log.Warn().WriteLine(ex, "Couldn't retrieve resources from {0}", loadedAssembly.GetName().Name);
             continue;
         }
         foreach (var resource in resources)
         {
             var resourceMatch = _assemblyResourceNameRegex.Match(resource);
             if (resourceMatch.Success)
             {
                 yield return(resourceMatch.Groups["assembly"].Value);
             }
         }
     }
 }
Exemplo n.º 3
0
        public static Type[] GetSubTypes <T>(string assemblyName = "")
        {
            IEnumerable <Assembly> assemblies = null;

            if (assemblyName.IsNotEmpty() &&
                LoadedAssemblies.Count(a => assemblyName.IsNotEmpty() && a.GetName().Name == assemblyName) > 0)
            {
                assemblies = LoadedAssemblies.Where(a => a.FullName.StartsWith(assemblyName));
            }
            else if (assemblyName.IsEmpty())
            {
                assemblies = LoadedAssemblies;
            }
            else
            {
                throw new InvalidOperationException($"The assembly {assemblyName} is not loaded.");
            }

            return(assemblies
                   .Select(a => a.GetTypes())
                   .SelectMany(t => t)
                   .Where(t => t.IsSubclassOf(typeof(T)) && !t.IsAbstract)?
                   .ToArray());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Do the one time scan of all the assemblies
        /// </summary>
        private void ScanForAssemblies()
        {
            var assemblies = new HashSet <AssemblyLocationInformation>();

            if (_applicationConfig.ScanForEmbeddedAssemblies)
            {
                foreach (var loadedAssembly in LoadedAssemblies.Where(pair => !AssembliesToIgnore.IsMatch(pair.Key)).Select(pair => pair.Value).ToList())
                {
                    string[] resources;
                    try
                    {
                        resources = Resources.GetCachedManifestResourceNames(loadedAssembly);
                    }
                    catch (Exception ex)
                    {
                        Log.Warn().WriteLine(ex, "Couldn't retrieve resources from {0}", loadedAssembly.GetName().Name);
                        continue;
                    }
                    foreach (var resource in resources)
                    {
                        var resourceMatch = _assemblyResourceNameRegex.Match(resource);
                        if (resourceMatch.Success)
                        {
                            assemblies.Add(new AssemblyLocationInformation(resourceMatch.Groups["assembly"].Value, loadedAssembly, resource));
                        }
                    }
                }
            }

            if (_applicationConfig.UseStrictChecking)
            {
                foreach (var applicationConfigScanDirectory in _applicationConfig.ScanDirectories)
                {
                    if (!Directory.Exists(applicationConfigScanDirectory))
                    {
                        throw new DirectoryNotFoundException(applicationConfigScanDirectory);
                    }
                }
            }
            foreach (var fileLocation in FileLocations.Scan(_applicationConfig.ScanDirectories, _assemblyFilenameRegex, SearchOption.TopDirectoryOnly))
            {
                assemblies.Add(new AssemblyLocationInformation(fileLocation.Item2.Groups["assembly"].Value, fileLocation.Item1));
            }

            // Reduce step 1) Take from the double assemblies only those which are embedded & on the file system in the probing path
            foreach (var assemblyGroup in assemblies.GroupBy(information => information.Name).ToList())
            {
                var groupList = assemblyGroup.ToList();
                if (groupList.Count <= 1)
                {
                    continue;
                }

                // Remove filesystem assemblies from the list which are not in the AssemblyResolveDirectories
                var unneededAssemblies = groupList.Where(info => !info.IsEmbedded && !info.IsOnProbingPath).ToList();
                if (groupList.Count - unneededAssemblies.Count < 1)
                {
                    continue;
                }

                foreach (var unneededAssemblyInformation in unneededAssemblies)
                {
                    assemblies.Remove(unneededAssemblyInformation);
                }
            }

            // Reduce step 2)
            foreach (var assemblyGroup in assemblies.GroupBy(information => information.Name).ToList())
            {
                var groupList = assemblyGroup.ToList();
                if (groupList.Count <= 1)
                {
                    continue;
                }
                // Remove assemblies which are older
                foreach (var unneededAssemblyInformation in groupList.OrderBy(info => info.FileDate).Skip(1).ToList())
                {
                    assemblies.Remove(unneededAssemblyInformation);
                }
            }

            // Create the assembly locations
            foreach (var assemblyLocationInformation in assemblies)
            {
                AvailableAssemblies[assemblyLocationInformation.Name] = assemblyLocationInformation;
            }
        }