public IAssemblyLoader Rewrite(string path) { bool hasSymbols = File.Exists(Path.ChangeExtension(path, ".pdb")); bool rewritten = false; var readerParameters = new ReaderParameters { ReadSymbols = hasSymbols, AssemblyResolver = _assemblyResolver, SymbolReaderProvider = hasSymbols ? new FixedPdbReaderProvider() : null }; using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(path, readerParameters)) { foreach (var module in assembly.Modules) { if (_moduleFilter.Accepts(module)) { IRewriteTargetMatcher targetCollection = _rewriteTargetCollection.GetMatcher(module); var types = module.GetTypes().ToList(); foreach (var type in types) { rewritten |= ProcessType(_configuration, type, targetCollection); } } } if (!rewritten) { // No changes made: we can just load the original assembly. return(new AssemblyLoader(path)); } // Run a number of post-processing operations on the assembly: module mvid generation, // assembly attribute filtering, etc. foreach (var postProcessor in _postProcessors) { postProcessor.Process(assembly); } string outputPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(path)); WriterParameters writerParameters = new WriterParameters { WriteSymbols = hasSymbols }; assembly.Write(outputPath, writerParameters); _rewrittenAssemblies.Add(outputPath); return(new AssemblyLoader(outputPath)); } }
public IAssemblyLoader Rewrite(string path) { bool hasSymbols = File.Exists(Path.ChangeExtension(path, ".pdb")); bool rewritten = false; ReaderParameters readerParameters = new ReaderParameters { ReadSymbols = hasSymbols }; AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(path, readerParameters); foreach (var module in assembly.Modules) { if (_moduleFilter.Accepts(module)) { IRewriteTargetMatcher targetCollection = _rewriteTargetCollection.GetMatcher(module); var types = module.GetTypes().ToList(); foreach (var type in types) { rewritten |= ProcessType(_configuration, type, targetCollection); } } } if (!rewritten) { // No changes made: we can just load the original assembly. return(new AssemblyLoader(path)); } string outputPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(path)); WriterParameters writerParameters = new WriterParameters { WriteSymbols = hasSymbols }; foreach (var module in assembly.Modules) { module.Mvid = Guid.NewGuid(); } assembly.Write(outputPath, writerParameters); _rewrittenAssemblies.Add(outputPath); return(new AssemblyLoader(outputPath)); }