从调试版本中的类型获取文件的源代码文件名

发布于 2024-09-30 01:42:59 字数 147 浏览 3 评论 0原文

给定 .NET 中的 Type 对象,我是否可以获得源代码文件名?我知道这仅在调试版本中可用,这很好,我也知道我可以使用 StackTrace 对象来获取调用堆栈中特定帧的文件名,但这不是我想要的。

但是我是否可以获取给定 System.Type 的源代码文件名?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怼怹恏 2024-10-07 01:42:59

我遇到了类似的问题。我需要仅使用类的类型和方法名称来查找特定方法的文件名和行号。我使用的是旧的 mono .net 版本(unity 4.6)。我使用了 cecil 库,它提供了一些帮助程序来分析您的 pbd(或 mono 的 mdb)并分析调试符号。 https://github.com/jbevain/cecil/wiki

对于给定的类型和方法:

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

我找到了这个解决方案:

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 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!

短叹 2024-10-07 01:42:59

考虑到至少 C# 支持分部类声明(例如,两个源代码文件中的 publicpartial class Foo),“给定 System.Type 的源代码文件名”意味着什么?如果类型声明分布在同一程序集中的多个源代码文件中,则源代码中不存在声明开始的单个点。

@Darin Dimitrov:这看起来不像“如何获取源文件名和类型成员的行号?”的重复。对我来说,因为这是关于类型成员,而这个问题是关于类型

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.

冰火雁神 2024-10-07 01:42:59

我使用这样的代码从正在运行的方法获取源文件和行号:

//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 + ")";

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 + ")";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文