public static void SaveAssembly(AssemblyDefinition assembly, string destination) { bool symbols = assembly.MainModule.HasSymbols; // re-write symbols, if available, so the new tokens will match assembly.Write(destination, new WriterParameters() { WriteSymbols = symbols }); if (symbols) { // re-load symbols (cecil will dispose MdbReader and will crash later if we need to save again) var provider = new MdbReaderProvider(); assembly.MainModule.ReadSymbols(provider.GetSymbolReader(assembly.MainModule, destination)); } else { // if we're not saving the symbols then we must not leave stale/old files to be used by other tools string dest_mdb = destination + ".mdb"; if (File.Exists(dest_mdb)) { File.Delete(dest_mdb); } } }
public static void SaveAssembly(AssemblyDefinition assembly, string destination) { var main = assembly.MainModule; bool symbols = main.HasSymbols; if (symbols) { var provider = new MdbReaderProvider(); main.ReadSymbols(provider.GetSymbolReader(main, main.FileName)); } var wp = new WriterParameters() { WriteSymbols = symbols }; // re-write symbols, if available, so the new tokens will match assembly.Write(destination, wp); if (!symbols) { // if we're not saving the symbols then we must not leave stale/old files to be used by other tools string dest_mdb = destination + ".mdb"; if (File.Exists(dest_mdb)) { File.Delete(dest_mdb); } } }
public void LoadFrom(FilePath assemblyPath) { FileName = assemblyPath; var tid = Runtime.SystemAssemblyService.GetTargetFrameworkForAssembly(Runtime.SystemAssemblyService.DefaultRuntime, assemblyPath); if (tid != null) { targetFramework = Runtime.SystemAssemblyService.GetTargetFramework(tid); } AssemblyDefinition adef = AssemblyDefinition.ReadAssembly(assemblyPath); MdbReaderProvider mdbProvider = new MdbReaderProvider(); try { ISymbolReader reader = mdbProvider.GetSymbolReader(adef.MainModule, assemblyPath); adef.MainModule.ReadSymbols(reader); } catch { // Ignore } var files = new HashSet <FilePath> (); foreach (TypeDefinition type in adef.MainModule.Types) { foreach (MethodDefinition met in type.Methods) { if (met.HasBody && met.Body.Instructions != null && met.Body.Instructions.Count > 0) { SequencePoint sp = met.Body.Instructions[0].SequencePoint; if (sp != null) { files.Add(sp.Document.Url); } } } } FilePath rootPath = FilePath.Empty; foreach (FilePath file in files) { AddFile(file, BuildAction.Compile); if (rootPath.IsNullOrEmpty) { rootPath = file.ParentDirectory; } else if (!file.IsChildPathOf(rootPath)) { rootPath = FindCommonRoot(rootPath, file); } } if (!rootPath.IsNullOrEmpty) { BaseDirectory = rootPath; } /* * foreach (AssemblyNameReference aref in adef.MainModule.AssemblyReferences) { * if (aref.Name == "mscorlib") * continue; * string asm = assemblyPath.ParentDirectory.Combine (aref.Name); * if (File.Exists (asm + ".dll")) * References.Add (new ProjectReference (ReferenceType.Assembly, asm + ".dll")); * else if (File.Exists (asm + ".exe")) * References.Add (new ProjectReference (ReferenceType.Assembly, asm + ".exe")); * else * References.Add (new ProjectReference (ReferenceType.Package, aref.FullName)); * }*/ }