Click here to Skip to main content
15,899,937 members
Home / Discussions / C#
   

C#

 
QuestionBest way to detect installed mail clients Pin
Spacedmc14-Jan-08 6:11
Spacedmc14-Jan-08 6:11 
GeneralReflection: Getting property name from property itself Pin
GazzaJ14-Jan-08 5:12
GazzaJ14-Jan-08 5:12 
GeneralRe: Reflection: Getting property name from property itself Pin
PIEBALDconsult14-Jan-08 5:24
mvePIEBALDconsult14-Jan-08 5:24 
GeneralRe: Reflection: Getting property name from property itself Pin
GazzaJ14-Jan-08 5:27
GazzaJ14-Jan-08 5:27 
GeneralRe: Reflection: Getting property name from property itself Pin
Ennis Ray Lynch, Jr.14-Jan-08 5:43
Ennis Ray Lynch, Jr.14-Jan-08 5:43 
GeneralRe: Reflection: Getting property name from property itself Pin
GazzaJ14-Jan-08 5:52
GazzaJ14-Jan-08 5:52 
GeneralRe: Reflection: Getting property name from property itself Pin
PIEBALDconsult14-Jan-08 8:33
mvePIEBALDconsult14-Jan-08 8:33 
GeneralRe: Reflection: Getting property name from property itself Pin
Judah Gabriel Himango14-Jan-08 5:54
sponsorJudah Gabriel Himango14-Jan-08 5:54 
You can do this using lambda expressions in C# 3. See http://themechanicalbride.blogspot.com/2007/03/symbols-on-steroids-in-c.html[^] for more info.

using System.Linq.Expressions;
public static class SymbolExtensions
{
    public static string GetPropertySymbol<T, R>(this T target, Expression<Func<T, R>> expression)
    {
        return ((MemberExpression)(((LambdaExpression)(expression)).Body)).Member.Name;
    }
}
You can then call it like this:

string propertyName = myObject.GetPropertySymbol(o => o.MyTestProperty);
myGrid.AddColumn(new ColumnDefn(propertyName, "", false, true));


Using this technique, you can get the name of a property or method without using strings at all. This can be beneficial in unit testing, INotifyPropertyChanged implementations, and other scenarios where hard-coding a string is a bad option.










Tech, life, family, faith: Give me a visit.
I'm currently blogging about: What this world needs... (Video)
The apostle Paul, modernly speaking: Epistles of Paul

Judah Himango


GeneralRe: Reflection: Getting property name from property itself Pin
GazzaJ14-Jan-08 5:57
GazzaJ14-Jan-08 5:57 
GeneralRe: Reflection: Getting property name from property itself Pin
Judah Gabriel Himango14-Jan-08 6:04
sponsorJudah Gabriel Himango14-Jan-08 6:04 
GeneralRe: Reflection: Getting property name from property itself Pin
DavidNohejl14-Jan-08 6:23
DavidNohejl14-Jan-08 6:23 
GeneralRe: Reflection: Getting property name from property itself Pin
S. Senthil Kumar14-Jan-08 6:18
S. Senthil Kumar14-Jan-08 6:18 
GeneralRe: Reflection: Getting property name from property itself [modified] Pin
PIEBALDconsult14-Jan-08 8:55
mvePIEBALDconsult14-Jan-08 8:55 
GeneralRe: Reflection: Getting property name from property itself Pin
GazzaJ14-Jan-08 23:39
GazzaJ14-Jan-08 23:39 
GeneralRe: Reflection: Getting property name from property itself Pin
DaveyM6914-Jan-08 23:56
professionalDaveyM6914-Jan-08 23:56 
GeneralRe: Reflection: Getting property name from property itself Pin
PIEBALDconsult15-Jan-08 12:03
mvePIEBALDconsult15-Jan-08 12:03 
GeneralRe: Reflection: Getting property name from property itself [modified] Pin
PIEBALDconsult15-Jan-08 12:54
mvePIEBALDconsult15-Jan-08 12:54 
GeneralRe: Reflection: Getting property name from property itself Pin
NassosReyzidis15-Jan-08 22:41
NassosReyzidis15-Jan-08 22:41 
QuestionIComparer Sorting multiple properties. Pin
Vijay Kulavade14-Jan-08 5:02
Vijay Kulavade14-Jan-08 5:02 
GeneralRe: IComparer Sorting multiple properties. Pin
Luc Pattyn14-Jan-08 5:40
sitebuilderLuc Pattyn14-Jan-08 5:40 
GeneralRe: IComparer Sorting multiple properties. Pin
Vijay Kulavade14-Jan-08 7:23
Vijay Kulavade14-Jan-08 7:23 
Generalput a folder Pin
angels77714-Jan-08 4:35
angels77714-Jan-08 4:35 
GeneralRe: put a folder Pin
Pete O'Hanlon14-Jan-08 5:12
mvePete O'Hanlon14-Jan-08 5:12 
GeneralRe: put a folder Pin
angels77714-Jan-08 14:04
angels77714-Jan-08 14:04 
GeneralRe: put a folder Pin
Pete O'Hanlon14-Jan-08 22:25
mvePete O'Hanlon14-Jan-08 22:25 

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

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