Einar Egilsson
Einar Egilsson

Reputation: 3488

Get source code filename of file from a type in debug build

Given a Type object in .NET, is it possible for me to get the source code filename? I know this would only be available in Debug builds and that's fine, I also know that I can use the StackTrace object to get the filename for a particular frame in a callstack, but that's not what I want.

But is it possible for me to get the source code filename for a given System.Type?

Upvotes: 6

Views: 3952

Answers (3)

Tichau
Tichau

Reputation: 305

I encountered a similar problem. I needed to found the file name and line number of a specific method with only the type of the class and the method name. I'm on an old mono .net version (unity 4.6). I used the library cecil, it provide some helper to analyse your pbd (or mdb for mono) and analyse debug symbols. https://github.com/jbevain/cecil/wiki

For a given type and method:

System.Type myType = typeof(MyFancyClass);
string methodName = "DoAwesomeStuff";

I found this solution:

Mono.Cecil.ReaderParameters readerParameters = new Mono.Cecil.ReaderParameters { ReadSymbols = true };
Mono.Cecil.AssemblyDefinition assemblyDefinition = Mono.Cecil.AssemblyDefinition.ReadAssembly(string.Format("Library\\ScriptAssemblies\\{0}.dll", assemblyName), readerParameters);

string fileName = string.Empty;
int lineNumber = -1;

Mono.Cecil.TypeDefinition typeDefinition = assemblyDefinition.MainModule.GetType(myType.FullName);
for (int index = 0; index < typeDefinition.Methods.Count; index++)
{
    Mono.Cecil.MethodDefinition methodDefinition = typeDefinition.Methods[index];
    if (methodDefinition.Name == methodName)
    {
        Mono.Cecil.Cil.MethodBody methodBody = methodDefinition.Body;
        if (methodBody.Instructions.Count > 0)
        {
            Mono.Cecil.Cil.SequencePoint sequencePoint = methodBody.Instructions[0].SequencePoint;
            fileName = sequencePoint.Document.Url;
            lineNumber = sequencePoint.StartLine;
        }
    }
}

I know it's a bit late :) but I hope this will help somebody!

Upvotes: 7

Don Boinske
Don Boinske

Reputation: 31

I've used code like this to get a source file and line number from the method that is running:

//must use the override with the single boolean parameter, set to true, to return additional file and line info

System.Diagnostics.StackFrame stackFrame = (new System.Diagnostics.StackTrace(true)).GetFrames().ToList().First();

//Careful:  GetFileName can return null even though true was specified in the StackTrace above.  This may be related to the OS on which this is running???

string fileName = stackFrame.GetFileName();
string process = stackFrame.GetMethod().Name + " (" + (fileName == null ? "" : fileName + " ") + fileLineNumber + ")";

Upvotes: 3

user
user

Reputation: 6947

Considering that at least C# supports partial class declarations (say, public partial class Foo in two source code files), what would "the source code filename for a given System.Type" mean? If a type declaration is spread out over multiple source code files within the same assembly, there is no single point in the source code where the declaration begins.

@Darin Dimitrov: This doesn't seem like a duplicate of "How to get the source file name and the line number of a type member?" to me, since that is about a type member, whereas this question is about a type.

Upvotes: 3

Related Questions