我不想再传递 nameof 了
2023-03-07 09:14:47 时间
有的时候抛出一个异常,我们需要知道是哪个方法抛出的异常。那么,我们可以通过传递 nameof 来获取调用者的方法名。但是,感觉很烦,每次都要传递 nameof。那么,有没有更好的方法呢?
CallerLineNumberAttribute
获取调用者的行号。
using System;
using System.Runtime.CompilerServices;
public static class Program
{
public static void Main()
{
TraceMessage("Something happened.");
}
public static void TraceMessage(string message,
[CallerLineNumber] int sourceLineNumber = 0)
{
Console.WriteLine("Line: {0} - {1}", sourceLineNumber, message);
}
}
// The example displays the following output:
// Line: 10 - Something happened.
CallerFilePathAttribute
获取调用者的文件路径。
using System;
using System.IO;
using System.Runtime.CompilerServices;
public static class Program
{
public static void Main()
{
TraceMessage("Something happened.");
}
public static void TraceMessage(string message,
[CallerFilePath] string sourceFilePath = "")
{
Console.WriteLine("File: {0} - {1}", Path.GetFileName(sourceFilePath), message);
}
}
// The example displays the following output:
// File: Program.cs - Something happened.
CallerMemberNameAttribute
获取调用者的方法名。
using System;
using System.Runtime.CompilerServices;
public static class Program
{
public static void Main()
{
DoProcessing();
}
public static void DoProcessing()
{
TraceMessage("Something happened.");
}
public static void TraceMessage(string message,
[CallerMemberName] string memberName = "")
{
Console.WriteLine("Member: {0} - {1}", memberName, message);
}
}
// The example displays the following output:
// Member: DoProcessing - Something happened.
CallerArgumentExpressionAttribute
获取调用者的参数表达式。C# 10.0 新增。
这个其实很好用,以后再也不用担心 ArgumentException 还需要写一个 nameof 了。
using System;
using System.Runtime.CompilerServices;
public static class Program
{
public static void Main()
{
int x = 10;
int y = 20;
Assert(x > y, "x > y");
}
public static void Assert(bool condition, [CallerArgumentExpression("condition")] string message = null)
{
Console.WriteLine("Condition: {0} - {1}", condition, message);
}
}
// The example displays the following output:
// Condition: False - x > y
总结
通过上面的几个例子,我们可以看到,借助在编译时获取调用者的行号、文件路劲和调用者方法名的特性,我们可以在开发中更加方便的进行日志记录。
参考
- CallerLineNumberAttribute Class^1
- CallerFilePathAttribute Class^2
- CallerMemberNameAttribute Class^3
- CallerArgumentExpressionAttribute Class^4
undefined
undefined
undefined
undefined
- 本文作者: newbe36524
- 本文链接: https://www.newbe.pro/Others/0x01D-I-don-t-want-to-pass-nameof-anymore/
- 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
- React 中的浅比较是如何工作的?
- Fedora 37 新功能披露
- 深入了解 EPUB 文件
- HarmonyOS应用开发:鸿蒙网络管理,网络请求获取天气信息
- 在 Manjaro 和其他基于 Arch Linux 的发行版上安装 Spotify
- 如何构建 Golang Dockerfiles?
- SpringCloud—使用分布式锁实现微服务重复请求控制
- Vue2剥丝抽茧-响应式系统之NextTick
- “作弊”:只需要知道这一个 Linux 命令就够了
- 火狐浏览器 Firefox 104 发布:新增网站耗电分析工具,可自动填写地址表单
- OpenHarmony - ArkUI(TS)开发之下拉选择菜单
- Linux 下让工作效率翻倍的四个实用技巧
- 在 GNOME 中创建你自定义的浅色和深色壁纸
- 16 图 | Nacos 架构原理①:一条注册请求会经历什么?
- 微软 Windows 11 22H2 Build 22621.457 (KB5016695) Release 预览版发布(附更新内容)
- 如何在 Linux 上设置私有 Git 服务器
- 如何在OpenHarmony上使用SeetaFace2人脸识别库?
- OpenHarmony - ArkUI(TS)声明式开发之画板
- 使用 LaTeX 创建优美的 PDF 文件
- 这些年搜集的 Linux 常用命令