前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >我不想再传递 nameof 了

我不想再传递 nameof 了

原创
作者头像
newbe36524
发布2023-02-16 08:24:25
1950
发布2023-02-16 08:24:25
举报

有的时候抛出一个异常,我们需要知道是哪个方法抛出的异常。那么,我们可以通过传递 nameof 来获取调用者的方法名。但是,感觉很烦,每次都要传递 nameof。那么,有没有更好的方法呢?

CallerLineNumberAttribute

获取调用者的行号。

代码语言:c#
复制
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

获取调用者的文件路径。

代码语言:c#
复制
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

获取调用者的方法名。

代码语言:c#
复制
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 了。

代码语言:c#
复制
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

总结

通过上面的几个例子,我们可以看到,借助在编译时获取调用者的行号、文件路劲和调用者方法名的特性,我们可以在开发中更加方便的进行日志记录。

参考

undefined

undefined

undefined

undefined

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CallerLineNumberAttribute
  • CallerFilePathAttribute
  • CallerMemberNameAttribute
  • CallerArgumentExpressionAttribute
  • 总结
  • 参考
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档