经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
我不想再传递 nameof 了
来源:cnblogs  作者:Newbe36524  时间:2023/2/17 10:01:37  对本文有异议

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

CallerLineNumberAttribute

获取调用者的行号。

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. public static class Program
  4. {
  5. public static void Main()
  6. {
  7. TraceMessage("Something happened.");
  8. }
  9. public static void TraceMessage(string message,
  10. [CallerLineNumber] int sourceLineNumber = 0)
  11. {
  12. Console.WriteLine("Line: {0} - {1}", sourceLineNumber, message);
  13. }
  14. }
  15. // The example displays the following output:
  16. // Line: 10 - Something happened.

CallerFilePathAttribute

获取调用者的文件路径。

  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. public static class Program
  5. {
  6. public static void Main()
  7. {
  8. TraceMessage("Something happened.");
  9. }
  10. public static void TraceMessage(string message,
  11. [CallerFilePath] string sourceFilePath = "")
  12. {
  13. Console.WriteLine("File: {0} - {1}", Path.GetFileName(sourceFilePath), message);
  14. }
  15. }
  16. // The example displays the following output:
  17. // File: Program.cs - Something happened.

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

CallerMemberNameAttribute

获取调用者的方法名。

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. public static class Program
  4. {
  5. public static void Main()
  6. {
  7. DoProcessing();
  8. }
  9. public static void DoProcessing()
  10. {
  11. TraceMessage("Something happened.");
  12. }
  13. public static void TraceMessage(string message,
  14. [CallerMemberName] string memberName = "")
  15. {
  16. Console.WriteLine("Member: {0} - {1}", memberName, message);
  17. }
  18. }
  19. // The example displays the following output:
  20. // Member: DoProcessing - Something happened.

CallerArgumentExpressionAttribute

获取调用者的参数表达式。C# 10.0 新增。

这个其实很好用,以后再也不用担心 ArgumentException 还需要写一个 nameof 了。

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. public static class Program
  4. {
  5. public static void Main()
  6. {
  7. int x = 10;
  8. int y = 20;
  9. Assert(x > y, "x > y");
  10. }
  11. public static void Assert(bool condition, [CallerArgumentExpression("condition")] string message = null)
  12. {
  13. Console.WriteLine("Condition: {0} - {1}", condition, message);
  14. }
  15. }
  16. // The example displays the following output:
  17. // Condition: False - x > y

总结

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

参考

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

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


  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.cnblogs.com/newbe36524/p/0x01D-I-don-t-want-to-pass-nameof-anymore.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号