I was asked how to get Mono.Cecil to rewrite PDB files so that VS can step through source when debugging.  Here’s an excerpt of the code I’m using to do this.  It works fine for me!

var assemblyResolver = new DefaultAssemblyResolver();
var assemblyLocation = Path.GetDirectoryName(AssemblyPath);
assemblyResolver.AddSearchDirectory(assemblyLocation);
if (!string.IsNullOrEmpty(HintPath))
{
assemblyResolver.AddSearchDirectory(HintPath);
}
var silverlightAssemblyPath = Environment.ExpandEnvironmentVariables(@”%ProgramFiles%\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\”);
assemblyResolver.AddSearchDirectory(silverlightAssemblyPath);
var readerParameters = new ReaderParameters { AssemblyResolver = assemblyResolver };
var writerParameters = new WriterParameters();
var pdbName = Path.ChangeExtension(AssemblyPath, “pdb”);
if (File.Exists(pdbName))
{
var symbolReaderProvider = new PdbReaderProvider();
readerParameters.SymbolReaderProvider = symbolReaderProvider;
readerParameters.ReadSymbols = true;
writerParameters.WriteSymbols = true;
}
var assemblyDefinition = AssemblyDefinition.ReadAssembly(AssemblyPath, readerParameters);
var weaver = new NotifyPropertyChangedWeaver();
weaver.Weave(assemblyDefinition);
assemblyDefinition.Write(AssemblyPath, writerParameters);

 

Advertisements