views:

72

answers:

3

I am writing a code analysis tool that uses reflection to validate a particular code base. When I encounter a type or member of interest I would like to load the symbols and extract the source file and line number where the member or type is defined. Is this possible? If so, how?

class SourceInfo
{
    public static SourceInfo GetFrom(MemberInfo member)
    {
        // What do I do here??
        throw new NotImplementedException();
    }

    public static SourceInfo GetFrom(Type member)
    {
        // What do I do here??
        throw new NotImplementedException();
    }

    public string SourceFilePath { get; private set; }
    public int LineNumber { get; private set; }
}
A: 

No, you can not do this using reflection. To do this, you need the associated program database file using the Symbol API.

Jason
+2  A: 

The available symbol APIs are listed in this blog article. I think the MDbg wrappers are your best bet for managed code. I only tried the DIA sdk and wasn't thrilled.

Hans Passant
+1  A: 

This codeproject article has some information about retrieving information from PDB file.

cmw