LookupReferencedAssembly() public method

public LookupReferencedAssembly ( Mono.Cecil.AssemblyNameReference name ) : LoadedAssembly
name Mono.Cecil.AssemblyNameReference
return LoadedAssembly
示例#1
0
            public PEFile Resolve(IAssemblyReference reference)
            {
                var module = parent.providedAssemblyResolver?.Resolve(reference);

                if (module != null)
                {
                    return(module);
                }
                return(parent.LookupReferencedAssembly(reference)?.GetPEFileOrNull());
            }
示例#2
0
 public PEFile Resolve(Decompiler.Metadata.IAssemblyReference reference)
 {
     return(parent.LookupReferencedAssembly(reference)?.GetPEFileOrNull());
 }
示例#3
0
            public AssemblyDefinition Resolve(AssemblyNameReference name)
            {
                var node = parent.LookupReferencedAssembly(name);

                return(node != null ? node.AssemblyDefinition : null);
            }
示例#4
0
 public AssemblyDefinition Resolve(AssemblyNameReference name)
 {
     return(parent.LookupReferencedAssembly(name)?.GetAssemblyDefinitionOrNull());
 }
示例#5
0
            public AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule)
            {
                var node = parent.LookupReferencedAssembly(assembly, sourceModule, true);

                return(node != null ? node.AssemblyDefinition : null);
            }
示例#6
0
        void WriteProjectFile(TextWriter writer, IEnumerable<Tuple<string, string>> files, LoadedAssembly assembly, DecompilationOptions options)
        {
            var module = assembly.ModuleDefinition;
            const string ns = "http://schemas.microsoft.com/developer/msbuild/2003";
            string platformName = GetPlatformName(module);
            Guid guid = (App.CommandLineArguments == null ? null : App.CommandLineArguments.FixedGuid) ?? Guid.NewGuid();
            using (XmlTextWriter w = new XmlTextWriter(writer)) {
                var asmRefs = GetAssemblyRefs(options, assembly);

                w.Formatting = Formatting.Indented;
                w.WriteStartDocument();
                w.WriteStartElement("Project", ns);
                w.WriteAttributeString("ToolsVersion", "4.0");
                w.WriteAttributeString("DefaultTargets", "Build");

                w.WriteStartElement("PropertyGroup");
                w.WriteElementString("ProjectGuid", (options.ProjectGuid ?? guid).ToString("B").ToUpperInvariant());

                w.WriteStartElement("Configuration");
                w.WriteAttributeString("Condition", " '$(Configuration)' == '' ");
                w.WriteValue("Debug");
                w.WriteEndElement(); // </Configuration>

                w.WriteStartElement("Platform");
                w.WriteAttributeString("Condition", " '$(Platform)' == '' ");
                w.WriteValue(platformName);
                w.WriteEndElement(); // </Platform>

                switch (module.Kind) {
                    case ModuleKind.Windows:
                        w.WriteElementString("OutputType", "WinExe");
                        break;
                    case ModuleKind.Console:
                        w.WriteElementString("OutputType", "Exe");
                        break;
                    default:
                        w.WriteElementString("OutputType", "Library");
                        break;
                }

                if (module.Assembly != null)
                    w.WriteElementString("AssemblyName", IdentifierEscaper.Escape(module.Assembly.Name));
                bool useTargetFrameworkAttribute = false;
                var targetFrameworkAttribute = module.Assembly == null ? null : module.Assembly.CustomAttributes.FirstOrDefault(a => a.TypeFullName == "System.Runtime.Versioning.TargetFrameworkAttribute");
                if (targetFrameworkAttribute != null && targetFrameworkAttribute.ConstructorArguments.Any()) {
                    string frameworkName = (targetFrameworkAttribute.ConstructorArguments[0].Value as UTF8String) ?? string.Empty;
                    string[] frameworkParts = frameworkName.Split(',');
                    string frameworkVersion = frameworkParts.FirstOrDefault(a => a.StartsWith("Version="));
                    if (frameworkVersion != null) {
                        w.WriteElementString("TargetFrameworkVersion", frameworkVersion.Substring("Version=".Length));
                        useTargetFrameworkAttribute = true;
                    }
                    string frameworkProfile = frameworkParts.FirstOrDefault(a => a.StartsWith("Profile="));
                    if (frameworkProfile != null)
                        w.WriteElementString("TargetFrameworkProfile", frameworkProfile.Substring("Profile=".Length));
                }
                if (!useTargetFrameworkAttribute) {
                    if (module.IsClr10) {
                        w.WriteElementString("TargetFrameworkVersion", "v1.0");
                    } else if (module.IsClr11) {
                        w.WriteElementString("TargetFrameworkVersion", "v1.1");
                    } else if (module.IsClr20) {
                        w.WriteElementString("TargetFrameworkVersion", "v2.0");
                        // TODO: Detect when .NET 3.0/3.5 is required
                    } else {
                        w.WriteElementString("TargetFrameworkVersion", "v4.0");
                    }
                }
                w.WriteElementString("WarningLevel", "4");

                w.WriteEndElement(); // </PropertyGroup>

                w.WriteStartElement("PropertyGroup"); // platform-specific
                w.WriteAttributeString("Condition", " '$(Platform)' == '" + platformName + "' ");
                w.WriteElementString("PlatformTarget", platformName);
                w.WriteEndElement(); // </PropertyGroup> (platform-specific)

                w.WriteStartElement("PropertyGroup"); // Debug
                w.WriteAttributeString("Condition", " '$(Configuration)' == 'Debug' ");
                w.WriteElementString("OutputPath", "bin\\Debug\\");
                w.WriteElementString("DebugSymbols", "true");
                w.WriteElementString("DebugType", "full");
                w.WriteElementString("Optimize", "false");
                if (options.DontReferenceStdLib) {
                    w.WriteStartElement("NoStdLib");
                    w.WriteString("true");
                    w.WriteEndElement();
                }
                w.WriteEndElement(); // </PropertyGroup> (Debug)

                w.WriteStartElement("PropertyGroup"); // Release
                w.WriteAttributeString("Condition", " '$(Configuration)' == 'Release' ");
                w.WriteElementString("OutputPath", "bin\\Release\\");
                w.WriteElementString("DebugSymbols", "true");
                w.WriteElementString("DebugType", "pdbonly");
                w.WriteElementString("Optimize", "true");
                if (options.DontReferenceStdLib) {
                    w.WriteStartElement("NoStdLib");
                    w.WriteString("true");
                    w.WriteEndElement();
                }
                w.WriteEndElement(); // </PropertyGroup> (Release)

                w.WriteStartElement("ItemGroup"); // References
                foreach (var r in asmRefs) {
                    if (r.Name != "mscorlib") {
                        var asm = assembly.LookupReferencedAssembly(r, module);
                        if (asm != null && ExistsInProject(options, asm.FileName))
                            continue;
                        w.WriteStartElement("Reference");
                        w.WriteAttributeString("Include", IdentifierEscaper.Escape(r.Name));
                        var hintPath = GetHintPath(options, asm);
                        if (hintPath != null) {
                            w.WriteStartElement("HintPath");
                            w.WriteString(hintPath);
                            w.WriteEndElement();
                        }
                        w.WriteEndElement();
                    }
                }
                w.WriteEndElement(); // </ItemGroup> (References)

                foreach (IGrouping<string, string> gr in (from f in files group f.Item2 by f.Item1 into g orderby g.Key select g)) {
                    w.WriteStartElement("ItemGroup");
                    foreach (string file in gr.OrderBy(f => f, StringComparer.OrdinalIgnoreCase)) {
                        w.WriteStartElement(gr.Key);
                        w.WriteAttributeString("Include", file);
                        w.WriteEndElement();
                    }
                    w.WriteEndElement();
                }

                w.WriteStartElement("ItemGroup"); // ProjectReference
                foreach (var r in asmRefs) {
                    var asm = assembly.LookupReferencedAssembly(r, module);
                    if (asm == null)
                        continue;
                    var otherProj = FindOtherProject(options, asm.FileName);
                    if (otherProj != null) {
                        var relPath = GetRelativePath(options.SaveAsProjectDirectory, otherProj.ProjectFileName);
                        w.WriteStartElement("ProjectReference");
                        w.WriteAttributeString("Include", relPath);
                        w.WriteStartElement("Project");
                        w.WriteString(otherProj.ProjectGuid.ToString("B").ToUpperInvariant());
                        w.WriteEndElement();
                        w.WriteStartElement("Name");
                        w.WriteString(IdentifierEscaper.Escape(otherProj.AssemblySimpleName));
                        w.WriteEndElement();
                        w.WriteEndElement();
                    }
                }
                w.WriteEndElement(); // </ItemGroup> (ProjectReference)

                w.WriteStartElement("Import");
                w.WriteAttributeString("Project", "$(MSBuildToolsPath)\\Microsoft.CSharp.targets");
                w.WriteEndElement();

                w.WriteEndDocument();
            }
        }