Last active
December 12, 2015 04:48
-
-
Save rmariuzzo/4716555 to your computer and use it in GitHub Desktop.
A better `ExpressionHelper.GetExpressionText` that handles Convert Expression.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Return the expression text | |
/// </summary> | |
/// <typeparam name="TModel"></typeparam> | |
/// <param name="expression"></param> | |
/// <returns></returns> | |
public static string GetExpressionText<TModel>(Expression<Func<TModel, object>> expression) | |
{ | |
var expr = (LambdaExpression)expression; | |
if (expr.Body.NodeType == ExpressionType.Convert) | |
{ | |
var ue = expr.Body as UnaryExpression; | |
var me = ((ue != null) ? ue.Operand : null) as MemberExpression; | |
return me.Member.Name; | |
} | |
return ExpressionHelper.GetExpressionText(expr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one, thanks :)
IMHO it makes the function a little more flexible if you change it to:
public static string GetExpressionText<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)