public DiscoveredAssembly(string path, MetadataReader reader)
        {
            var definition = reader.GetAssemblyDefinition();

            this.Path = path;

            this.Name = reader.GetString(definition.Name);

            this.ReferencedAssemblies = reader.AssemblyReferences
                                        .Select(r => new { Token = reader.GetToken(r), AssemblyReference = reader.GetAssemblyReference(r) })
                                        .Select(tr => DiscoveredAssembly.FromReference(tr.Token, reader.GetString(tr.AssemblyReference.Name)));

            this.DefinedTypes = reader.TypeDefinitions
                                .Select(t => DiscoveredType.FromDefinedType(reader, t));

            this.ReferencedTypes = reader.TypeReferences
                                   .Select(r => new { Token = reader.GetToken(r), TypeReference = reader.GetTypeReference(r) })
                                   .Select(tr =>
                                           DiscoveredType.FromReferenceType(
                                               tr.Token,
                                               reader.GetString(tr.TypeReference.Name),
                                               reader.GetString(tr.TypeReference.Namespace)));

            var allTypes      = this.DefinedTypes.Union(this.ReferencedTypes);
            var resolvedTypes = new List <DiscoveredType>();

            foreach (var type in allTypes)
            {
                resolvedTypes.Add(type.Resolve(allTypes));
            }
            this.ResolvedTypes = resolvedTypes;
        }
        private DiscoveredType(MetadataReader reader, TypeDefinitionHandle handle)
        {
            var definition = reader.GetTypeDefinition(handle);

            this.Token      = reader.GetToken(handle);
            this.Name       = reader.GetString(definition.Name);
            this.Namespace  = reader.GetString(definition.Namespace);
            this.Interfaces = definition.GetInterfaceImplementations()
                              .Select(i => reader.GetInterfaceImplementation(i).Interface)
                              .Select(i => DiscoveredType.FromInterface(reader.GetToken(i)));
            this.isLocal = true;
        }
Beispiel #3
0
        private IEnumerable <AssemblyScanResult <T> > DiscoverAssemblies(string startingPath, IEnumerable <string> searchPatterns, string typeToFind, string namespaceToFind)
        {
            var results    = new List <AssemblyScanResult <T> >();
            var assemblies = new List <DiscoveredAssembly>();

            foreach (var directoryPath in Directory.GetDirectories(startingPath))
            {
                var files = searchPatterns.SelectMany(p => ExcludeRuntimesFolder(Directory.GetFiles(directoryPath, p, SearchOption.AllDirectories)));
                foreach (var assemblyFilePath in files)
                {
                    assemblies.Add(DiscoverAssembly(assemblyFilePath));
                }
            }

            DiscoveredType foundType     = null;
            var            resolvedTypes = new List <DiscoveredType>();

            foreach (var assembly in assemblies)
            {
                assembly.Resolve(assemblies);
                resolvedTypes.AddRange(assembly.DefinedTypes);

                var type = assembly.DefinedTypes.FirstOrDefault(t => t.Name == typeToFind && t.Namespace == namespaceToFind);
                if (type != null && foundType == null)
                {
                    foundType = type;
                }
            }

            if (foundType == null)
            {
                throw new AssemblyScanningException($"{typeToFind} was not found using DiscoveryAssemblyScanner");
            }

            foreach (var assembly in assemblies)
            {
                // Iterate all found plugin types
                foreach (var type in assembly.ResolvedTypes.Where(t => t.Interfaces != null && t.Interfaces.Any(i => i?.Name == foundType.Name)))
                {
                    results.Add(new AssemblyScanResult <T>
                    {
                        AssemblyName        = Path.GetFileName(assembly.Path),
                        AssemblyPath        = Path.GetDirectoryName(assembly.Path),
                        PluginTypeName      = type.Name,
                        PluginTypeNamespace = type.Namespace
                    });
                }
            }
            return(results);
        }