C# 中是否存在 __LINE__ __FILE__ 等价物? [英] Do __LINE__ __FILE__ equivalents exist in C#?

查看:35
本文介绍了C# 中是否存在 __LINE__ __FILE__ 等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于记录目的

__LINE__ 
__FILE__ 

是我的 C/C++ 朋友.在 Java 中,为了获取该信息,我必须抛出异常并捕获它.为什么这些旧的备用数据库在现代编程语言中如此被忽视?它们的简单有一些神奇之处.

were my friends in C/C++. In Java to get that information I had to throw an exception and catch it. Why are these old standbys so neglected in the modern programming languages? There is something magical about their simplicity.

推荐答案

它更丑,但您可以在 C# 中使用 StackTraceStackFrame 类:

It is uglier, but you can do something like this in C# using the StackTrace and StackFrame classes:

StackTrace st = new StackTrace(new StackFrame(true));
Console.WriteLine(" Stack trace for current level: {0}", st.ToString());
StackFrame sf = st.GetFrame(0);
Console.WriteLine(" File: {0}", sf.GetFileName());
Console.WriteLine(" Method: {0}", sf.GetMethod().Name);
Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());
Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());

当然,这会带来一些开销.

Of course, this comes with some overhead.

这篇关于C# 中是否存在 __LINE__ __FILE__ 等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆