欢迎光临散文网 会员登陆 & 注册

我不想再传递 nameof 了

2023-02-16 08:22 作者:Newbe36524  | 我要投稿



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




## CallerLineNumberAttribute


获取调用者的行号。


```csharp

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


获取调用者的文件路径。


```csharp

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.

```


> **可发帖可群聊的技术交流方式已经上线,欢迎通过链接,加入我们一起讨论。 <https://www.newbe.pro/links/>**


## CallerMemberNameAttribute


获取调用者的方法名。


```csharp

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 了。


```csharp

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](https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.callerlinenumberattribute?view=net-7.0&WT.mc_id=DX-MVP-5003606)[^1]

- [CallerFilePathAttribute Class](https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.callerfilepathattribute?view=net-7.0&WT.mc_id=DX-MVP-5003606)[^2]

- [CallerMemberNameAttribute Class](https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.callermembernameattribute?view=net-7.0&WT.mc_id=DX-MVP-5003606)[^3]

- [CallerArgumentExpressionAttribute Class](https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.callerargumentexpressionattribute?view=net-7.0&WT.mc_id=DX-MVP-5003606)[^4]


[^1]: https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.callerlinenumberattribute?view=net-7.0&WT.mc_id=DX-MVP-5003606

[^2]: https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.callerfilepathattribute?view=net-7.0&WT.mc_id=DX-MVP-5003606

[^3]: https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.callermembernameattribute?view=net-7.0&WT.mc_id=DX-MVP-5003606

[^4]: https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.callerargumentexpressionattribute?view=net-7.0&WT.mc_id=DX-MVP-5003606


**感谢您的阅读,如果您觉得本文有用,请点赞、关注和转发。**


> **可发帖可群聊的技术交流方式已经上线,欢迎通过链接,加入我们一起讨论。 <https://www.newbe.pro/links/>**



- 本文作者: [newbe36524](https://www.newbe.pro)

- 本文链接: [https://www.newbe.pro/Others/0x01D-I-don-t-want-to-pass-nameof-anymore/](https://www.newbe.pro/Others/0x01D-I-don-t-want-to-pass-nameof-anymore/)

- 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!


我不想再传递 nameof 了的评论 (共 条)

分享到微博请遵守国家法律