我怎样才能获得在C#中调用函数的名字吗? [英] How can I get the name of the calling function in C#?

查看:346
本文介绍了我怎样才能获得在C#中调用函数的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C#中调用函数的名称。我读到的StackFrame方法,但它无处不表示,其不可靠的,伤害的性能。

I need to get the name of the calling function in C#. I read about stackframe method but everywhere its said its not reliable and hurts the performance.

但我需要它用于生产。我写这将记录从跟踪被称为好方法的名称自定义示踪剂。

But I need it for production use. I am writing a custom tracer which will be logging the name of the method from which trace was called as well.

可能有人请用有效的方法来获取调用帮助功能的名称?我使用Visual Studio 2010。

Could someone please help with a efficient method to get the calling function name? I am using Visual Studio 2010.

推荐答案

升级到C#5,并使用 CallerMemberName

upgrade to c# 5 and use CallerMemberName

public void Method([CallerMemberName] string caller="")
{
}

修改

其他好东西用C#5

public void Method([CallerMemberName] string name="",
                   [CallerLineNumber] int line=-1,
                   [CallerFilePath] string path="")
{
}

这篇关于我怎样才能获得在C#中调用函数的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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