手机
当前位置:查字典教程网 >编程开发 >C#教程 >C# 获取属性名的方法
C# 获取属性名的方法
摘要:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;...

复制代码 代码如下:

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# 获取属性名的方法】相关文章:

用C#对ADO.NET数据库完成简单操作的方法

C#实现获取不同对象中名称相同属性的方法

c#中分割字符串的几种方法

c#动态调用Webservice的两种方法实例

C# 语音功能的实现方法

c# 托盘双击不触发单击事件的实现方法

C# WINFORM 强制让窗体获得焦点的方法代码

c# 可选参数、命名参数

用C#编写获取远程IP,MAC的方法

解决C# winForm自定义鼠标样式的两种实现方法详解

精品推荐
分类导航