Exemplo n.º 1
0
        public void Parse()
        {
            using (var stream = new FileStream(FilePath, FileMode.Open))
            {
                var per = new PEReader(stream);
                if (!per.HasMetadata)
                {
                    Name = Name.ToLowerInvariant() + ".dll";
                    return;
                    //per.PEHeaders.PEHeader.ImportTableDirectory.
                }
                var mdr = per.GetMetadataReader();

                //mdr.GetModuleReference
                foreach (var af in mdr.AssemblyFiles.Select(mdr.GetAssemblyFile))
                {
                }
                foreach (var ar in mdr.AssemblyReferences.Select(mdr.GetAssemblyReference))
                {
                    AssemblyReferences.Add(mdr.GetString(ar.Name));
                }
                foreach (var xt in mdr.ExportedTypes.Select(mdr.GetExportedType))
                {
                    if (xt.Implementation.Kind == HandleKind.AssemblyReference)
                    {
                        var           ar  = mdr.GetAssemblyReference((AssemblyReferenceHandle)xt.Implementation);
                        var           arn = mdr.GetString(ar.Name);
                        List <string> types;
                        if (!TypeForwarding.TryGetValue(arn, out types))
                        {
                            TypeForwarding[arn] = types = new List <string>();
                        }
                        types.Add(mdr.GetString(xt.Name));
                    }
                }
                foreach (var tr in mdr.TypeReferences.Select(mdr.GetTypeReference))
                {
                    if (tr.ResolutionScope.Kind == HandleKind.AssemblyReference)
                    {
                        var ar  = mdr.GetAssemblyReference((AssemblyReferenceHandle)tr.ResolutionScope);
                        var arn = mdr.GetString(ar.Name);
                        var trn = mdr.GetString(tr.Name);
                        if (trn.StartsWith("Assembly") && trn.EndsWith("Attribute"))
                        {
                            continue;
                        }
                        List <string> types;
                        if (!TypeReferencing.TryGetValue(arn, out types))
                        {
                            TypeReferencing[arn] = types = new List <string>();
                        }
                        types.Add(trn);
                    }
                    else
                    {
                        //Console.WriteLine("{0}", tr.ResolutionScope.Kind);
                    }
                }
                foreach (var td in mdr.TypeDefinitions.Select(mdr.GetTypeDefinition))
                {
                    var tdn = mdr.GetString(td.Name);
                    if ((td.Attributes & TypeAttributes.Public) == TypeAttributes.Public &&
                        !tdn.StartsWith("<"))
                    {
                        TypeDefinitions.Add(tdn);
                    }
                    foreach (var md in td.GetMethods().Select(mdr.GetMethodDefinition))
                    {
                        var imp = md.GetImport();
                        if (!imp.Module.IsNil)
                        {
                            var mr  = mdr.GetModuleReference(imp.Module);
                            var mrn = mdr.GetString(mr.Name);
                            if (!mrn.EndsWith(".dll"))
                            {
                                mrn = mrn + ".dll";
                            }
                            if (!DllImports.Contains(mrn))
                            {
                                DllImports.Add(mrn);
                            }
                        }
                    }
                }
            }
        }