Getting Mono.Cecil to rewrite PDB files to enable debugging

January 19, 2011

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

3 Responses to “Getting Mono.Cecil to rewrite PDB files to enable debugging”

  1. David Turczi said

    Where is PdbReaderProvider class located? I added Mono.Cecil and Mono.Cecil.Mdb to references…

    Also will I not encounter dll locking with this? You don’t seem to be using ShadowCopying.

    Thanks again for your unbelieveably fast response!

  2. johnhmarks said

    You need to add a reference to the Mono.Cecil.Pdb assembly.

  3. David Turczi said

    Thanks, I overcome all other minor obstacles (including where to find Mono.Cecil.Pdb 🙂 )! It works like a charm! I owe you a drink or something 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: