我不想再傳遞 nameof 了

有的時候拋出一個異常,我們需要知道是哪個方法拋出的異常。那么,我們可以通過傳遞 nameof 來獲取調(diào)用者的方法名。但是,感覺很煩,每次都要傳遞 nameof。那么,有沒有更好的方法呢?
## CallerLineNumberAttribute
獲取調(diào)用者的行號。
```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
獲取調(diào)用者的文件路徑。
```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.
```
> **可發(fā)帖可群聊的技術(shù)交流方式已經(jīng)上線,歡迎通過鏈接,加入我們一起討論。 <https://www.newbe.pro/links/>**
## CallerMemberNameAttribute
獲取調(diào)用者的方法名。
```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
獲取調(diào)用者的參數(shù)表達式。C# 10.0 新增。
這個其實很好用,以后再也不用擔(dān)心 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
```
## 總結(jié)
通過上面的幾個例子,我們可以看到,借助在編譯時獲取調(diào)用者的行號、文件路勁和調(diào)用者方法名的特性,我們可以在開發(fā)中更加方便的進行日志記錄。
## 參考
- [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
**感謝您的閱讀,如果您覺得本文有用,請點贊、關(guān)注和轉(zhuǎn)發(fā)。**
> **可發(fā)帖可群聊的技術(shù)交流方式已經(jīng)上線,歡迎通過鏈接,加入我們一起討論。 <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/)
- 版權(quán)聲明: 本博客所有文章除特別聲明外,均采用 BY-NC-SA 許可協(xié)議。轉(zhuǎn)載請注明出處!