[CSharpTips]C# 获取当前方法名

获取当前方法名

添加System.Runtime.CompilerServices 命名空间

添加 "GetCallerName"方法如下,在方法中调用此方法便可获取当前方法名

复制代码
 1 using System;
 2 using System.Runtime.CompilerServices;
 3 
 4 namespace GetCallerNameTest
 5 {
 6     internal class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Console.WriteLine($"Current Function is {GetCallerName()}");
11             Console.ReadLine();
12         }
13 
14         static string GetCallerName([CallerMemberName] string callerName = "")
15         {
16             return callerName;
17         }
18     }
19 }
复制代码

输出

 

posted @   xiaoshuye  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示