C# 獲取屬性名的方法
更新時間:2013年03月01日 11:11:33 作者:
C# 獲取屬性名的方法實例,需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
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;
}
}
}
}
相關(guān)文章
C#使用GZipStream解壓縮數(shù)據(jù)文件的方法
這篇文章主要介紹了C#使用GZipStream解壓縮數(shù)據(jù)文件的方法,實例分析了C#中GZipStream方法的原理與使用技巧,需要的朋友可以參考下2015-04-04
基于C# 寫一個 Redis 數(shù)據(jù)同步小工具
Redis支持主從同步。數(shù)據(jù)可以從主服務(wù)器向任意數(shù)量的從服務(wù)器上同步,從服務(wù)器可以是關(guān)聯(lián)其他從服務(wù)器的主服務(wù)器。這篇文章主要介紹了用 C# 寫一個 Redis 數(shù)據(jù)同步小工具,需要的朋友可以參考下2020-02-02
基于使用BeginInvoke,EndInvoke異步調(diào)用委托的實現(xiàn)代碼
本篇文章是對使用BeginInvoke,EndInvoke異步調(diào)用委托的實現(xiàn)代碼進行了分析介紹,需要的朋友參考下2013-05-05

