C# 获取属性名的方法
时间:2023-03-05 07:02:18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
class Test
{
public string PropertyJustForTest1 { get; set; }
public Test PropertyJustForTest2 { get; set; }
}
static void Main(string[] args)
{
Test test = new Test();
Console.WriteLine(GetPropertyNameHelper.GetPropertyName<object>(() => test.PropertyJustForTest1));
Console.WriteLine(GetPropertyNameHelper.GetPropertyName<object>(() => test.PropertyJustForTest2));
}
}
static class GetPropertyNameHelper
{
public static string GetPropertyName<T>(Expression<Func<T>> express)
{
var memberExpress = express.Body as MemberExpression;
if (memberExpress != null)
{
return memberExpress.Member.Name;
}
else
{
return string.Empty;
}
}
}
}
标签:C#,获取属性名
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决ThingsBoard编译报错问题:Failure to find org.gradle:gradle-tooling-api:jar:6.3
2021-11-20 16:24:22
![](https://img.aspxhome.com/file/2023/7/62117_0s.png)
springboot自定义异常视图过程解析
2023-06-29 09:44:36
![](https://img.aspxhome.com/file/2023/3/71173_0s.png)
浅谈Spring Cloud Ribbon的原理
2023-07-23 04:11:25
![](https://img.aspxhome.com/file/2023/6/76506_0s.png)
C# Winform实现石头剪刀布游戏
2022-03-17 17:05:14
详解Java变量与常量
2023-11-09 21:00:14
![](https://img.aspxhome.com/file/2023/5/59005_0s.png)
Android ADB常用命令总结
2022-05-24 18:07:42
Kotlin中协程的创建过程详析
2023-11-27 07:45:16
![](https://img.aspxhome.com/file/2023/8/130418_0s.png)
非常实用的小功能 Android应用版本的更新实例
2023-10-28 03:41:51
初识MyBatis及基本配置和执行
2021-11-12 05:53:40
![](https://img.aspxhome.com/file/2023/7/75487_0s.jpg)
IntelliJ IDEA 常用设置(配置)吐血整理(首次安装必需)
2021-06-24 15:23:49
![](https://img.aspxhome.com/file/2023/8/84278_0s.png)
c# 使用线程对串口serialPort进行收发数据(四种)
2023-06-20 13:05:48
![](https://img.aspxhome.com/file/2023/5/119585_0s.jpg)
Java 详解垃圾回收与对象生命周期
2022-01-21 02:54:43
C#枚举类型与位域枚举Enum
2023-03-02 06:52:27
android 使用Xml文件定义Shape方式
2022-09-08 19:19:38
![](https://img.aspxhome.com/file/2023/5/102585_0s.jpg)
java多线程编程之慎重使用volatile关键字
2022-09-08 00:36:41
SpringBoot消息国际化配置实现过程解析
2023-05-16 01:19:22
![](https://img.aspxhome.com/file/2023/5/67155_0s.png)
Android7.0 MessageQueue详解
2021-08-29 17:01:43
![](https://img.aspxhome.com/file/2023/0/103870_0s.jpg)
Java毕业设计实战之共享租车信息管理系统的实现
2022-08-02 13:37:32
![](https://img.aspxhome.com/file/2023/5/72525_0s.jpg)
SpringBoot自动装配原理详解
2023-07-03 05:49:08
JPype实现在python中调用JAVA的实例
2022-08-24 03:19:52