Esempio n. 1
0
        private static TypeVerificationResult TestTypeRefs(IEnumerable <TypeReference> typeRefs)
        {
            TypeVerificationResult result = new TypeVerificationResult();

            foreach (var typeRef in typeRefs)
            {
                if (typeRef.Scope.MetadataScopeType == MetadataScopeType.AssemblyNameReference)
                {
                    TypeDefinition typeDef = null;
                    try
                    {
                        typeDef = typeRef.Resolve();
                    }
                    catch (AssemblyResolutionException)
                    {
                        typeDef = null;
                    }

                    var assemblyNameRef = (AssemblyNameReference)typeRef.Scope;
                    if (typeDef == null)
                    {
                        var unresolvedType = Tuple.Create(assemblyNameRef.Name, typeRef.FullName);
                        if (!result.UnresolvedTypes.Contains(unresolvedType))
                        {
                            result.UnresolvedTypes.Add(unresolvedType);
                        }
                    }
                    else
                    {
                        var resolvedType = Tuple.Create(assemblyNameRef.Name, typeDef.Module.Name, typeDef.FullName);
                        if (!result.ResolvedTypes.Contains(resolvedType))
                        {
                            result.ResolvedTypes.Add(resolvedType);
                        }
                        result.ModuleLocations[typeDef.Module.Name] = typeDef.Module.FileName;
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
        private static void VerifyTypesRefs(FileSystemInfo f, DirectoryInfo[] frameworkPaths, bool showOnlyErrors = false)
        {
            AssemblyDefinitionFromAssembly(f, frameworkPaths, out string[] searchDirectories, out AssemblyDefinition assembly);

            // Iterate through each module in the assembly and validate type-refs
            TypeVerificationResult result1 = null;

            foreach (var module in assembly.Modules.Union(Enumerable.Repeat(assembly.MainModule, 1)))
            {
                var typeRefs = module.GetTypeReferences();
                result1 = TestTypeRefs(typeRefs);
            }

            // Iterate over assembly-level attributes and validate the type-refs
            // that are found at [assembly:] scope
            List <TypeReference> assemblyTypeRefs = new List <TypeReference>();

            foreach (var attr in assembly.CustomAttributes)
            {
                assemblyTypeRefs.Add(attr.AttributeType);
                foreach (var cargs in attr.ConstructorArguments)
                {
                    assemblyTypeRefs.Add(cargs.Type);
                }
                foreach (var flds in attr.Fields)
                {
                    assemblyTypeRefs.Add(flds.Argument.Type);
                }
            }
            var result2 = TestTypeRefs(assemblyTypeRefs);

            // Print out the results
            //
            // Errors first
            // Successes next
            // Module -> DLL map
            // Search paths used.
            //
            foreach (var type in result1.UnresolvedTypes.Union(result2.UnresolvedTypes))
            {
                Console.WriteLine($"ERROR: [{type.Item1}] {type.Item2}");
            }

            Console.WriteLine();
            if (!showOnlyErrors)
            {
                foreach (var type in result1.ResolvedTypes.Union(result2.ResolvedTypes))
                {
                    Console.WriteLine($"Resolved: [{type.Item1}]->[{type.Item2}] {type.Item3}");
                }
            }

            Console.WriteLine();
            Console.WriteLine("Module locations:");
            foreach (var module in result1.ModuleLocations.Union(result2.ModuleLocations))
            {
                Console.WriteLine($"\t{module.Key}: {module.Value}");
            }

            Console.WriteLine();
            Console.WriteLine("Assembly Resolve Search Directories:");
            foreach (var dir in searchDirectories)
            {
                Console.WriteLine($"\t{dir}");
            }
        }