Пример #1
0
        internal static IEnumerable <DerivedTypesEntryNode> FindDerivedTypes(AssemblyList list, ITypeDefinition type,
                                                                             CancellationToken cancellationToken)
        {
            var definitionMetadata = type.ParentModule.PEFile.Metadata;
            var metadataToken      = (SRM.TypeDefinitionHandle)type.MetadataToken;
            var assemblies         = list.GetAllAssemblies().GetAwaiter().GetResult()
                                     .Select(node => node.GetPEFileOrNull()).Where(asm => asm != null).ToArray();

            foreach (var module in assemblies)
            {
                var metadata = module.Metadata;
                var assembly = (MetadataModule)module.GetTypeSystemOrNull().MainModule;
                foreach (var h in metadata.TypeDefinitions)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var td = metadata.GetTypeDefinition(h);
                    foreach (var iface in td.GetInterfaceImplementations())
                    {
                        var ifaceImpl = metadata.GetInterfaceImplementation(iface);
                        if (!ifaceImpl.Interface.IsNil && IsSameType(metadata, ifaceImpl.Interface, definitionMetadata, metadataToken))
                        {
                            yield return(new DerivedTypesEntryNode(list, assembly.GetDefinition(h)));
                        }
                    }
                    SRM.EntityHandle baseType = td.GetBaseTypeOrNil();
                    if (!baseType.IsNil && IsSameType(metadata, baseType, definitionMetadata, metadataToken))
                    {
                        yield return(new DerivedTypesEntryNode(list, assembly.GetDefinition(h)));
                    }
                }
            }
            yield break;
        }
Пример #2
0
        IEnumerable <PEFile> GetReferencingModules(PEFile self, CancellationToken ct)
        {
            yield return(self);

            string reflectionTypeScopeName = typeScope.Name;

            if (typeScope.TypeParameterCount > 0)
            {
                reflectionTypeScopeName += "`" + typeScope.TypeParameterCount;
            }

            var toWalkFiles  = new Stack <PEFile>();
            var checkedFiles = new HashSet <PEFile>();

            toWalkFiles.Push(self);
            checkedFiles.Add(self);
            IList <LoadedAssembly> assemblies = AssemblyList.GetAllAssemblies().GetAwaiter().GetResult();

            do
            {
                PEFile curFile = toWalkFiles.Pop();
                foreach (var assembly in assemblies)
                {
                    ct.ThrowIfCancellationRequested();
                    bool found  = false;
                    var  module = assembly.GetPEFileOrNull();
                    if (module == null || !module.IsAssembly)
                    {
                        continue;
                    }
                    if (checkedFiles.Contains(module))
                    {
                        continue;
                    }
                    var resolver = assembly.GetAssemblyResolver(loadOnDemand: false);
                    foreach (var reference in module.AssemblyReferences)
                    {
                        if (resolver.Resolve(reference) == curFile)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found && checkedFiles.Add(module))
                    {
                        if (ModuleReferencesScopeType(module.Metadata, reflectionTypeScopeName, typeScope.Namespace))
                        {
                            yield return(module);
                        }
                        if (ModuleForwardsScopeType(module.Metadata, reflectionTypeScopeName, typeScope.Namespace))
                        {
                            toWalkFiles.Push(module);
                        }
                    }
                }
            } while (toWalkFiles.Count > 0);
        }
Пример #3
0
        IEnumerable <PEFile> GetModuleAndAnyFriends(ITypeDefinition typeScope, CancellationToken ct)
        {
            var self = typeScope.ParentModule.PEFile;

            yield return(self);

            var typeProvider = MetadataExtensions.MinimalAttributeTypeProvider;
            var attributes   = self.Metadata.CustomAttributes.Select(h => self.Metadata.GetCustomAttribute(h))
                               .Where(ca => ca.GetAttributeType(self.Metadata).GetFullTypeName(self.Metadata).ToString() == "System.Runtime.CompilerServices.InternalsVisibleToAttribute");
            var friendAssemblies = new HashSet <string>();

            foreach (var attribute in attributes)
            {
                string assemblyName = attribute.DecodeValue(typeProvider).FixedArguments[0].Value as string;
                assemblyName = assemblyName.Split(',')[0];                 // strip off any public key info
                friendAssemblies.Add(assemblyName);
            }

            if (friendAssemblies.Count > 0)
            {
                IEnumerable <LoadedAssembly> assemblies = AssemblyList.GetAllAssemblies()
                                                          .GetAwaiter().GetResult();

                foreach (var assembly in assemblies)
                {
                    ct.ThrowIfCancellationRequested();
                    if (friendAssemblies.Contains(assembly.ShortName))
                    {
                        var module = assembly.GetPEFileOrNull();
                        if (module == null)
                        {
                            continue;
                        }
                        if (ModuleReferencesScopeType(module.Metadata, typeScope.Name, typeScope.Namespace))
                        {
                            yield return(module);
                        }
                    }
                }
            }
        }
Пример #4
0
 public IEnumerable <PEFile> GetAllModules()
 {
     return(AssemblyList.GetAllAssemblies().GetAwaiter().GetResult()
            .Select(asm => asm.GetPEFileOrNull()));
 }