vendredi 13 mars 2015

Cannot get this property name using this code and expression api

I have the following class and I need to get its property names:



public class PMLButtonData
{
public int BackgroundColorID
{
get;
set;
}

public string Callback
{
get;
set;
}
}


To get the names I'm using this function



public static string GetPropertyName<T>(Expression<Func<T, object>> lambda)
{
MemberExpression member = lambda.Body as MemberExpression;
PropertyInfo property = member.Member as PropertyInfo;

return property.Name;
}


I can get the Callback property name using this code:



string name = GetPropertyName<PMLButtonData>(x => x.Callback);


But the same code for the other property doesn't work:



string name = GetPropertyName<PMLButtonData>(x => x.BackgroundColorID);


The only difference between them is the data type, so I changed the Callback to int and the code does not work with this property anymore. Why can't I get the name of a property this way if it's an integer?






Aucun commentaire:

Enregistrer un commentaire