您的位置:首页 > 编程语言

怎么获取调用当前函数的代码位置

2016-02-26 09:50 399 查看
用StackTrace 来获取。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
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.ReadLine();
}
}
}


本人验证有效
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: