__FUNCTION__ 宏的 C# 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/259536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# version of __FUNCTION__ macro
提问by Filip Fr?cz
Does anyone has a good solution for a C# version of the C++ __FUNCTION__ macro? The compiler does not seem to like it.
有没有人对 C++ __FUNCTION__ 宏的 C# 版本有好的解决方案?编译器似乎不喜欢它。
采纳答案by Eoin Campbell
Try using this instead.
尝试改用它。
System.Reflection.MethodBase.GetCurrentMethod().Name
C# doesn't have __LINE__
or __FUNCTION__
macros like C++ but there are equivalents
C#不具有__LINE__
或__FUNCTION__
像C ++宏,但也有等效
回答by e.James
The following should work, although it will be evaluated at runtime instead of during compilation.
以下应该可以工作,尽管它将在运行时而不是在编译期间进行评估。
System.Reflection.MethodBase.GetCurrentMethod().Name
回答by JaredPar
Unfortunately there is no equivalent version of that macro in C#. I don't consider the GetCurrentMethodName() solution equivalent to the C++ __FUNCTION__ macro. Namely becase the C++ version is a compile time computation of the name. For C# this is a runtime calculation and incurs a performance hit.
不幸的是,C# 中没有该宏的等效版本。我不认为 GetCurrentMethodName() 解决方案等同于 C++ __FUNCTION__ 宏。即因为 C++ 版本是名称的编译时计算。对于 C#,这是一个运行时计算并会导致性能下降。
I'm not making any assumtions about the severity of the cost but there is one
我没有对成本的严重程度做任何假设,但有一个
回答by Mark Booth
What I currently use is a function like this:
我目前使用的是这样的函数:
using System.Diagnostics;
public string __Function() {
StackTrace stackTrace = new StackTrace();
return stackTrace.GetFrame(1).GetMethod().Name;
}
When I need __FUNCTION__, I just call the __Function() instead. For example:
当我需要 __FUNCTION__ 时,我只调用 __Function()。例如:
Debug.Assert(false, __Function() + ": Unhandled option");
Of course this solution uses reflection too, but it is the best option I can find. Since I only use it for Debugging (not Tracing in release builds) the performance hit is not important.
当然,这个解决方案也使用了反射,但它是我能找到的最好的选择。由于我只将它用于调试(不在发布版本中进行跟踪),因此性能影响并不重要。
I guess what I should do is create debug functions and tag them with
我想我应该做的是创建调试函数并标记它们
[ Conditional("Debug") ]
instead, but I haven't got around to that.
相反,但我还没有解决这个问题。
Thanks to Jeff Mastry for his solutionto this.
感谢 Jeff Mastry 对此的解决方案。
回答by ShloEmi
I use this:
我用这个:
public static string CallerName([CallerMemberName] string callerName = "")
{
return callerName;
}
Usage example:
用法示例:
s_log.DebugFormat("{0}", CallerName());
The down side of using it is that every time you want to print the caller name, you need to jump to the function ==> time consuming & performance hit! So, I use it for debugging perpose and if I need to print also in production code, I usually inline the function name into the log.Debug, e.g. :
使用它的缺点是每次要打印调用者名称时,都需要跳转到函数 ==> 耗时 & 性能下降!所以,我用它来调试 perpose,如果我还需要在生产代码中打印,我通常将函数名称内联到 log.Debug 中,例如:
s_log.Debug("CallerName");
HTH..
哈..
回答by jm.
This is added in .NET 4.5.
这是在 .NET 4.5 中添加的。
See @roken's answer here:
在此处查看@roken 的回答: