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 );
}
相关推荐
考虑考虑16 小时前
JDK26中的LazyConstant
java·后端·java ee
水无痕simon16 小时前
1. Guava 介绍
开发语言·python·guava
Devin~Y16 小时前
互联网大厂 Java 面试实录:JVM、Spring Boot、MyBatis、Redis、Kafka、Spring AI、K8s 全链路追问小Y
java·jvm·spring boot·redis·kafka·mybatis·spring security
摇滚侠16 小时前
SpringCloud 面试题 真正的 offer 偏方 Java 基础 Java 高级
java·spring·spring cloud
AI科技星16 小时前
全域数学公理:基于32维超复数与易经卦爻的宇宙大一统理论(整理版)
c语言·开发语言·线性代数·量子计算·agi
之歆16 小时前
DAY_13JavaScript DOM 操作完全指南:实战案例、性能优化与业务价值(下)
开发语言·前端·javascript·性能优化·ecmascript
tongluowan00716 小时前
Java 内存模型(JMM)- happens-before 与内存屏障
java·内存模型·happens-before
plainGeekDev17 小时前
Android Framework 面试题:Binder都说不清楚,简历别写精通了
android·java
Gauss松鼠会17 小时前
【GaussDB】基于SpringBoot实现操作GaussDB(DWS)的项目实战
java·数据库·经验分享·spring boot·后端·sql·gaussdb
Gauss松鼠会17 小时前
【GaussDB】GaussDB 常见问题及解决方案汇总
java·数据库·算法·性能优化·gaussdb·经验总结