Apologies for spamming this site.  I finally managed to get the info out of 
Cecil that I was after.

The follow code can be used to iterate through all classes and methods and 
dump out their Source File location.

My issue was loading the symbols. Other posts indicated that once the 
assembly definition was loaded, all I had to do was call 
asm.MainModule.ReadSymbols()

However, this caused errors for me.  

I had to load the symbol file separately and then use this to load the 
symbols for the MainModule, as follows:

            MonoSymbolFile symFile = 
MonoSymbolFile.ReadSymbolFile(monoAssembly + ".mdb");
            ISymbolReader sr = (ISymbolReader)(new 
Mono.Cecil.Mdb.MdbReader(asm.MainModule, symFile));
            asm.MainModule.ReadSymbols(sr);

Hopefully the code below will save someone else time mucking around.

*I still have an outstanding question I am hoping someone can answer for 
me.  *

*I would like to use the  MonoSymbolFile.Methods, a MethodEntry list. 
MethodEntry has the source file.  Where it fails for me, is that 
GetRealName often returns a null.  How can I retrieve the source file 
location for a MethodEntry?*



========================================================


using System;
using System.Collections.Generic;
using System.Text;
using Mono.Cecil;
using Mono.Cecil.Metadata;
using Mono.Cecil.Mdb;
using Mono.Cecil.Cil;
using Mono.CompilerServices.SymbolWriter;
using System.Diagnostics;

namespace TestCecil
{
    public class Program
    {


        public static void DumpAssembly(string monoAssembly)
        {

            // Loads the assembly from the start arguments
            var readerParameters = new ReaderParameters { ReadSymbols = 
true };
            AssemblyDefinition asm = 
AssemblyDefinition.ReadAssembly(monoAssembly, readerParameters);

            // Load Symbols
            MonoSymbolFile symFile = 
MonoSymbolFile.ReadSymbolFile(monoAssembly + ".mdb");
            ISymbolReader sr = (ISymbolReader)(new 
Mono.Cecil.Mdb.MdbReader(asm.MainModule, symFile));
            asm.MainModule.ReadSymbols(sr);

            // Iterates through the assembly's modules, aspects of the 
program
            foreach (ModuleDefinition mod in asm.Modules)
            {
                MonoSymbolFile msf = MonoSymbolFile.ReadSymbolFile(mod);
                
                Log(mod.Name);
                // Iterates through the classes of the assembly
                foreach (TypeDefinition td in mod.Types)
                {
                    // Writes the types name, 
                    DoType(td);
                }
            }
        }

        static void DoType(TypeDefinition td)
        {
            foundModule = false;
                {
                    foundModule = true;
                    foreach (TypeDefinition ntd in td.NestedTypes)
                    {
                        // Repeat if the class has nested classes.
                        DoType(ntd);
                    }
                    foreach (MethodDefinition md in td.Methods)
                    {
                        // Log the content to the console
                        Log(td.Name + " > " + md.Name);

                        foreach (var l in md.Body.Instructions)
                        {
                            if (l.SequencePoint != null)
                            {
                                if (l.SequencePoint.Document != null)
                                {
                                    if (l.SequencePoint.Document.Url != 
null)
                                    {
                                        
Debug.WriteLine(l.SequencePoint.Document.Url);
                                        break;
                                    }
                                }
                            }
                        }
                    

                    }
                }
        }

    }
}

-- 
-- 
--
mono-cecil
--- 
You received this message because you are subscribed to the Google Groups 
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to