How to get access to embedded assembly's metadata using IMetaDataDispenser.OpenScope?
See the question and my original answer on StackOverflowI don't think you can do this using the .NET Framework builtin functions, as they rely on physical files. However, there is a solution using the Mono Cecil library, as it has an overloads that takes a Stream as input instead of a file path for its symbols reader.
Here is an example of a Console app named "TestPdb" which dumps its IL code to the console, including PDB information:
using System;
using System.IO;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Pdb;
namespace TestPdb
{
class Program
{
static void Main(string[] args)
{
// we use a Stream for the assembly
AssemblyDefinition asm;
using (FileStream asmStream = new FileStream("testpdb.exe", FileMode.Open, FileAccess.Read, FileShare.Read))
{
asm = AssemblyDefinition.ReadAssembly(asmStream);
}
// we use a Stream for the PDB
using (FileStream symbolStream = new FileStream("testpdb.pdb", FileMode.Open, FileAccess.Read, FileShare.Read))
{
asm.MainModule.ReadSymbols(new PdbReaderProvider().GetSymbolReader(asm.MainModule, symbolStream));
}
TypeDefinition type = asm.MainModule.GetType("TestPdb.Program");
foreach (MethodDefinition method in type.Methods)
{
Console.WriteLine("Method:" + method.Name);
foreach (Instruction ins in method.Body.Instructions)
{
Console.WriteLine(" " + ins);
if (ins.SequencePoint != null)
{
Console.WriteLine(" Url:" + ins.SequencePoint.Document.Url);
// see http://blogs.msdn.com/b/jmstall/archive/2005/06/19/feefee-sequencepoints.aspx
if (ins.SequencePoint.StartLine != 0xFEEFEE)
{
Console.WriteLine(" StartLine:" + ins.SequencePoint.StartLine + " StartColumn:" + ins.SequencePoint.StartColumn);
Console.WriteLine(" EndLine:" + ins.SequencePoint.EndLine + " EndColumn:" + ins.SequencePoint.EndColumn);
}
// etc...
}
}
}
}
}
}
NOTE: since you only need to read from PDBs, you can recompile the Cecil library defining the READ_ONLY conditional symbol to save some bytes. You can also embed Cecil source code directly in your assemblies, no need to use the .DLL versions.