Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to get property name using Expression

, 16 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to get property name using Expression
Expression Trees
Expression trees represent code in a tree-like data structure where each node is an expression, for example, a method call or a binary operation such as x < y.
Using the above feature, we can retrieve the property name of class:
class County
  int CountryCode;
  string CountryName;
To retreive the name of property: "CountryName" of class, we build the following expression:
Country c = new Country();
Expression<Func<string>> exp =() => c.ContryName;
string propName = exp.Body.ToString().Substring(exp.Body.ToString().LastIndexOf('.') + 1);
Use of the GetProperty in this case is redundant, because we need to know the property name to call the method.
Please let me know if it is useful.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


United States United States
No Biography provided

Comments and Discussions

GeneralRe: In which case, congrats (and thanks)! Keep it up. Especially... PinmemberJonathon Bolster16-Dec-11 10:40 
GeneralHow can you get the property name? Both CountryCode and Coun... PinmemberTiefeng You19-Dec-11 11:54 
GeneralRe: Take a look at this blog post on static reflection: http://b... PinmemberFréderic Cordier24-Dec-11 4:15 
GeneralSometimes you have to use a string. For example, I use NHibe... PinmemberJonathon Bolster16-Dec-11 5:14 
GeneralRe: Thanks Jonathon for explaining the utility. This is the firs... PinmemberARanadhir16-Dec-11 7:48 
GeneralExcuse me, i don't understand the utility ?? PinmemberYeurl_200716-Dec-11 4:53 
QuestionI'm curious as to how this is usefull? PinmemberDaniel Gidman21-Dec-11 9:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411022.1 | Last Updated 16 Dec 2011
Article Copyright 2011 by ARanadhir
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid