C# 获取入参函数名

前言

C# 如何通过形参获得实参的名字?
C# 10 新特性 ------ CallerArgumentExpression

我最近在研究Godot,想简化Godot的操作。所有有了这个需求

csharp 复制代码
public static void Test(string str){
	console.wirteLine(nameof(str));
}
public static void Main(string[] args) {
    string newStr = "hello";
    Test(newStr);

}

打印结果

复制代码
//输出
str
//我想要的输出
newStr

最后找了好久,终于找到了最新的C#10新特性,解决了此问题

C#10新特性

C# 10 新特性 ------ CallerArgumentExpression

这里不多讲了,结果就是可以拿到形参的名称。

csharp 复制代码
public static void Test(string str,[CallerArgumentExpression(nameof(node))] string nameExpression = null){
	//这样就能拿到形参了
	console.wirteLine(nameExpression );
}
相关推荐
sali-tec1 小时前
C# 基于halcon的视觉工作流-章56-彩图转云图
人工智能·算法·计算机视觉·c#
qq_5470261792 小时前
Flowable 工作流引擎
java·服务器·前端
鼓掌MVP3 小时前
Java框架的发展历程体现了软件工程思想的持续进化
java·spring·架构
编程爱好者熊浪3 小时前
两次连接池泄露的BUG
java·数据库
lllsure3 小时前
【Spring Cloud】Spring Cloud Config
java·spring·spring cloud
鬼火儿4 小时前
SpringBoot】Spring Boot 项目的打包配置
java·后端
NON-JUDGMENTAL4 小时前
Tomcat 新手避坑指南:环境配置 + 启动问题 + 乱码解决全流程
java·tomcat
大佬,救命!!!5 小时前
C++多线程同步与互斥
开发语言·c++·学习笔记·多线程·互斥锁·同步与互斥·死锁和避免策略
chxii5 小时前
Maven 详解(上)
java·maven
李少兄5 小时前
IntelliJ IDEA 远程调试(Remote Debugging)教程
java·ide·intellij-idea