C#获取当前的类名,函数,命名空间
dotnet 命令行创建项目
| dotnet new console -n <项目名称> |
| dotnet new console -n LogOut |
Program.cs
| using LogOut.Utils; |
| var res = Calculator.Add(20, 30); |
| Console.WriteLine(res); |
Calculate.cs
关键代码:
| MethodBase currentMethod = MethodBase.GetCurrentMethod()!; |
| |
| string namespaceName = currentMethod.DeclaringType!.Namespace!; |
| |
| string className = currentMethod.DeclaringType.Name; |
| |
| string methodName = currentMethod.Name; |
| |
| |
| Console.WriteLine("当前执行的方法的信息:"); |
| Console.WriteLine("命名空间:{0}", namespaceName); |
| Console.WriteLine("类名:{0}", className); |
| Console.WriteLine("方法名:{0}", methodName); |
LogOut.Utils.Calculator.cs
| |
| using System.Reflection; |
| |
| namespace LogOut.Utils |
| { |
| public class Calculator |
| { |
| |
| |
| public static double Add(double a, double b) |
| { |
| |
| MethodBase currentMethod = MethodBase.GetCurrentMethod()!; |
| |
| string namespaceName = currentMethod.DeclaringType!.Namespace!; |
| |
| string className = currentMethod.DeclaringType.Name; |
| |
| string methodName = currentMethod.Name; |
| |
| |
| Console.WriteLine("当前执行的方法的信息:"); |
| Console.WriteLine("命名空间:{0}", namespaceName); |
| Console.WriteLine("类名:{0}", className); |
| Console.WriteLine("方法名:{0}", methodName); |
| return a + b; |
| } |
| } |
| } |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
2022-09-21 api_post
2022-09-21 fastapi使用mongodb的小demo
2022-09-21 flask 博客系统 逻辑关系
2022-09-21 flask中使用pymongo