关于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.NET4中的使用介绍方法
时间:2022-03-27 22:05:56
C# 5.0 给我们带来了三个非常有用的编译器特性
CallerMemberName
CallerFilePath
CallerLineNumber
在C与C++中由下列字符帮助我们实现调试消息的文件行号
.#define debug_msg printf("%s[%d]:",__FILE__,__LINE__);printf
在.NET 4中与其功能相等的是
new StackTrace(true).GetFrame(1).GetMethod().Name(注意,是功能相等,但实现不同,.NET4中是运行时获取,而C#5.0 中应该是编译时指定,原因参考以下)
在C#5.0中我们可以用以下代码实现调试信息文件行号获取:
public static void TraceMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Trace.WriteLine("message: " + message);
Trace.WriteLine("member name: " + memberName);
Trace.WriteLine("source file path: " + sourceFilePath);
Trace.WriteLine("source line number: " + sourceLineNumber);
}
用VS2012编译调试,便能看见文件,行号,调用者方法名称。
三个特性是.NET 4.5里面的,如果在.NET4中使用那么请定义一下特性:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class CallerMemberNameAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class CallerFilePathAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class CallerLineNumberAttribute : Attribute { }
}
为了编译时.NET4和.NET4.5兼容,可以用预处理指令增加编译条件,在4.5下编译以上代码。
关键点来了,在.NET4下定义以上属性后,用VS2010编译,无相关信息输出,
用VS2012重新编译,则会输出相关信息(注意实在.NET4下),说明这个特性是编译器特性。也就是说我们可以在VS2012里写.NET4项目时用以上特性。
标签:callermembername,.NET4,C#,5.0
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android微信SDK实现分享
2021-09-18 17:52:58
C#中SQL Command的基本用法
2023-10-04 06:01:32
java后端进行跨域的几种方式小结
2021-09-03 14:53:31
IDEA配置JRebel实现热部署的方法
2022-08-28 20:53:00
![](https://img.aspxhome.com/file/2023/1/100051_0s.png)
Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
2022-01-20 22:21:12
![](https://img.aspxhome.com/file/2023/8/100598_0s.png)
Java实现配置加载机制
2023-11-26 09:03:38
详解Java利用深度优先遍历解决迷宫问题
2022-08-20 02:46:54
Java泛型之类型擦除实例详解
2022-02-13 06:39:36
![](https://img.aspxhome.com/file/2023/0/86720_0s.png)
elasticsearch集群cluster示例详解
2023-12-11 16:49:27
![](https://img.aspxhome.com/file/2023/3/67003_0s.png)
C# Winform实现自定义漂亮的通知效果
2021-08-10 08:15:29
关于maven打包时的报错: Return code is: 501 , ReasonPhrase:HTTPS Required
2022-09-09 00:50:51
![](https://img.aspxhome.com/file/2023/0/74900_0s.png)
SpringBoot配置Email发送功能实例
2022-03-07 20:52:20
![](https://img.aspxhome.com/file/2023/3/61943_0s.jpg)
SpringBoot项目读取外置logback配置文件的问题及解决
2023-06-06 10:50:22
![](https://img.aspxhome.com/file/2023/3/68563_0s.png)
微信js-sdk+JAVA实现“分享到朋友圈”和“发送给朋友”功能详解
2021-10-20 23:13:51
Java如何基于IO流实现同一文件读写操作
2023-07-30 15:06:38
SpringBoot嵌入式Servlet容器与定制化组件超详细讲解
2023-03-31 09:07:05
![](https://img.aspxhome.com/file/2023/5/75795_0s.png)
一文带你彻底理解Java序列化和反序列化
2021-12-23 05:24:12
spring boot容器启动流程
2023-02-17 09:25:36
![](https://img.aspxhome.com/file/2023/3/77143_0s.png)
C#结束进程及子进程
2021-06-22 15:18:10
IDEA2020.2.3中创建JavaWeb工程的完整步骤记录
2022-03-15 05:16:02
![](https://img.aspxhome.com/file/2023/5/110605_0s.png)