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 );
}
相关推荐
码力斜杠哥9 分钟前
Rust初习录(6)Rust的 if 玩法
开发语言·python·rust
luck_bor11 分钟前
集合进阶(Collections Set List)
java
敲敲千反田13 分钟前
Spring AI
java·人工智能·spring
聆风吟º14 分钟前
【C标准库】深入理解C语言 isalpha 函数详解:判断字符是否为字母
c语言·开发语言·库函数·isalpha
拽着尾巴的鱼儿26 分钟前
spring 动态代理
java·后端·spring
gf132111130 分钟前
python_【更新已发送的消息卡片】
java·前端·python
WL_Aurora35 分钟前
Java字符输入全攻略
java·开发语言
Hello.Reader1 小时前
算法基础(十三)——随机算法为什么有时主动引入随机性
java·数据库·算法
likerhood1 小时前
ConcurrentHashMap底层数据结构和面试常见问题
java·数据结构·面试·hashmap
茉莉玫瑰花茶1 小时前
LangGraph 拓展核心知识点
开发语言·windows·python