C# 獲取屬性名的方法
更新時(shí)間:2013年03月01日 11:11:33 作者:
C# 獲取屬性名的方法實(shí)例,需要的朋友可以參考一下
復(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ù)文件的方法,實(shí)例分析了C#中GZipStream方法的原理與使用技巧,需要的朋友可以參考下2015-04-04
基于C# 寫一個(gè) Redis 數(shù)據(jù)同步小工具
Redis支持主從同步。數(shù)據(jù)可以從主服務(wù)器向任意數(shù)量的從服務(wù)器上同步,從服務(wù)器可以是關(guān)聯(lián)其他從服務(wù)器的主服務(wù)器。這篇文章主要介紹了用 C# 寫一個(gè) Redis 數(shù)據(jù)同步小工具,需要的朋友可以參考下2020-02-02
C#實(shí)現(xiàn)封裝常用Redis工具類的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)封裝常用Redis工具類的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼
這篇文章主要介紹了C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
C#中累加器函數(shù)Aggregate用法實(shí)例
這篇文章主要介紹了C#中累加器函數(shù)Aggregate用法,實(shí)例分析了C#中累加器的實(shí)現(xiàn)與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
基于使用BeginInvoke,EndInvoke異步調(diào)用委托的實(shí)現(xiàn)代碼
本篇文章是對(duì)使用BeginInvoke,EndInvoke異步調(diào)用委托的實(shí)現(xiàn)代碼進(jìn)行了分析介紹,需要的朋友參考下2013-05-05

