In a class, I have a static method to fetch the PropertyInfo by supplying an expression.
public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> expressie)
{
MemberExpression me;
switch (expressie.Body.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
var ue = expressie.Body as UnaryExpression;
me = ((ue != null) ? ue.Operand : null) as MemberExpression;
break;
default:
me = expressie.Body as MemberExpression;
break;
}
if (me == null)
{
throw new InvalidOperationException("Expression does not refer to a property: " + expressie.ToString());
}
return (PropertyInfo)me.Member;
}
To make my code more type safe, I changed the expressions that I'm using in part of the methods which make a call to this function.
// Old
Expression<Func<T, Object>> expression;
// New (TProp instead of Object)
Expression<Func<T, TProp>> expression;
As a result, I can't use the old 'GetPropertyInfo' method anymore, because the expression does not match the requested parameters. So I created a new one.
public static PropertyInfo GetPropertyInfo<T, TProp>(Expression<Func<T, TProp>> expressie)
{
MemberExpression me;
switch (expressie.Body.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
var ue = expressie.Body as UnaryExpression;
me = ((ue != null) ? ue.Operand : null) as MemberExpression;
break;
default:
me = expressie.Body as MemberExpression;
break;
}
if (me == null)
{
throw new InvalidOperationException("Expression does not refer to a property: " + expressie.ToString());
}
return (PropertyInfo)me.Member;
}
The body of this new expression is exactly the same as the existing one. Because I would like to stick with the DRY principle, I searched for a way to use one method body for both cases. This is possible by first converting the 'Expression<Func<T, TProp>>' into 'Expression<Func<T, Object>>' and passing the converted expression to the 'old' GetPropertyInfo(...) method. I am not able to completely remove the old 'GetPropertyInfo' method, because of other pieces of code still relying on it.
The conversion method that I'm using is the following:
private static Expression<Func<T, object>> convertToObjectExpression<T, TProp>(Expression<Func<T, TProp>> expression)
{
return Expression.Lambda<Func<T,object>>(
Expression.Convert(expression.Body, typeof(object)),
expression.Parameters);
}
This works fine, except when I'm using the expression with a nullable DateTime (DateTime?) as type of TProp. In that case the following line results in 'me' being null.
me = ((ue != null) ? ue.Operand : null) as MemberExpression;
Does anybody have any idea what can cause this problem?
I have a fiddle that contains the problem.
I would suggest you just go the other way round. Do not relay from your new typesafe method to the old one (with all attached problems), but from the old one to the new method:
// relay old interface to new one for compatibility
public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> expressie)
{
return GetPropertyInfo<T, object>(expressie);
}
public static PropertyInfo GetPropertyInfo<T, TProp>(Expression<Func<T, TProp>> expressie)
{ /* your code, as you provided it */ }
If you still got problems with this, please provide your calling code
Edit
removed for being irrelevant to the problem
Does anybody have any idea what can cause this problem?
The way you called the new function (with DateTime? cast from a normal DateTime property) and the way you implemented it (via convertToObjectExpression) leads to having two Convert expressions, hence the problem.
In order to fix it, consider the user grek40 advice, and/or change the implementation as follows
public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> expressie)
{
var body = expressie.Body;
while (body.NodeType == ExpressionType.Convert || body.NodeType == ExpressionType.ConvertChecked)
body = ((UnaryExpression)body).Operand;
var me = body as MemberExpression;
var property = me != null ? me.Member as PropertyInfo : null;
if (property == null)
{
throw new InvalidOperationException("Expression does not refer to a property: " + expressie.ToString());
}
return property;
}
Related
I'm developing a API that uses lambda expressions to specify properties. I'm using this famous piece of code similar to this one (this is simplified and incomplete, just to make clear what I'm talking about):
public void Foo<T, P>(Expression<Func<T, P>> action)
{
var expression = (MemberExpression)action.Body;
string propertyName = expression.Member.Name;
// ...
}
To be called like this:
Foo((String x) => x.Length);
Now I would like to specify a property path by chaining property names, like this:
Foo((MyClass x) => x.Name.Length);
Foo should be able to split the path into its property names ("Name" and "Length"). Is there a way to do this with reasonable effort?
There is a somehow similar looking question, but I think they are trying to combine lambda expressions there.
Another question also is dealing with nested property names, but I don't really understand what they are talking about.
Something like this?
public void Foo<T, P>(Expression<Func<T, P>> expr)
{
MemberExpression me;
switch (expr.Body.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
var ue = expr.Body as UnaryExpression;
me = ((ue != null) ? ue.Operand : null) as MemberExpression;
break;
default:
me = expr.Body as MemberExpression;
break;
}
while (me != null)
{
string propertyName = me.Member.Name;
Type propertyType = me.Type;
Console.WriteLine(propertyName + ": " + propertyType);
me = me.Expression as MemberExpression;
}
}
I played a little with ExpressionVisitor:
public static class PropertyPath<TSource>
{
public static IReadOnlyList<MemberInfo> Get<TResult>(Expression<Func<TSource, TResult>> expression)
{
var visitor = new PropertyVisitor();
visitor.Visit(expression.Body);
visitor.Path.Reverse();
return visitor.Path;
}
private class PropertyVisitor : ExpressionVisitor
{
internal readonly List<MemberInfo> Path = new List<MemberInfo>();
protected override Expression VisitMember(MemberExpression node)
{
if (!(node.Member is PropertyInfo))
{
throw new ArgumentException("The path can only contain properties", nameof(node));
}
this.Path.Add(node.Member);
return base.VisitMember(node);
}
}
}
Usage:
var path = string.Join(".", PropertyPath<string>.Get(x => x.Length).Select(p => p.Name));
Old question, I know... but if it's only the names you need, an even simpler way to do it is:
expr.ToString().Split('.').Skip(1)
EDIT:
public class A
{
public B Property { get; set; }
}
public class B
{
public C field;
}
[Fact]
public void FactMethodName()
{
var exp = (Expression<Func<A, object>>) (x => x.Property.field);
foreach (var part in exp.ToString().Split('.').Skip(1))
Console.WriteLine(part);
// Output:
// Property
// field
}
I have a shared .NET standard DTO between client and server and expressions are a great way to create query strings that can be rebuilt and executed Api side.
A perfect way to create Type safe queries over the wire.
I digress, I needed a property path also
x => x.Siblings.Age
To produce a string like
"Siblings.Age"
I went with this
public static string GetMemberPath(MemberExpression me)
{
var parts = new List<string>();
while (me != null)
{
parts.Add(me.Member.Name);
me = me.Expression as MemberExpression;
}
parts.Reverse();
return string.Join(".", parts);
}
public static string GetPath<T, TProperty>(this Expression<Func<T, TProperty>> exp)
{
return string.Join(".", GetItemsInPath(exp).Reverse());
}
private static IEnumerable<string> GetItemsInPath<T, TProperty>(Expression<Func<T, TProperty>> exp)
{
if (exp == null)
{
yield break;
}
var memberExp = FindMemberExpression(exp.Body);
while (memberExp != null)
{
yield return memberExp.Member.Name;
memberExp = FindMemberExpression(memberExp.Expression);
}
}
I refactored these answers to use recursion, so no need for order reversal. I only need the property tree, so left out all but MemberExpressions. Should be simple to add functionality if need be.
public IEnumerable<string> PropertyPath<TModel, TValue>(
Expression<Func<TModel, TValue>> expression)
{
if (expression.Body is MemberExpression memberExpression)
return PropertyPathRecurse(memberExpression);
return Enumerable.Empty<string>();
}
private IEnumerable<string> PropertyPathRecurse(MemberExpression? expression)
{
if (expression is null)
return Enumerable.Empty<string>();
return PropertyPathRecurse(expression.Expression as MemberExpression)
.Append(expression.Member.Name);
}
I'm trying to do a generic method that would accept an order by parameter that I could then inspect for it's name and attributes before building the query and sending it to the database.
I was thinking of something like:
public class SearchEngine<T>
{
// Removed other parameters to make it simple
public IEnumerable<T> Search<K>(Func<T, K> orderBy)
{
throw new NotImplementedException();
}
}
I was hoping to consume it later with:
var searchEngine = new SearchEngine<User>();
var result = searchEngine.Search(x => x.Name);
My goal is to get a hold, inside of the search method, of that property so that I could get the name of the property, "Name" in this case, and even after it, use reflection to get its attributes to get other information I have setup.
I know about getting the attributes, what I'm not sure is how can I get the info of the property being passed. I'm sure Linq does this in some way, I just don't know how.
If I tried something like:
var result = searchEngine.Search<PropertyInfo>(x => x.Name);
This wouldnt work since the parameter is returning a string in this case.
Any ideas would be appreciated.
Use expression tree and change Search method parameter type to Expression<Func<T, K>>:
public IEnumerable<T> Search<K>(Expression<Func<T, K>> orderBy)
{
var memberExpression = orderBy.Body as MemberExpression;
if (memberExpression == null)
throw new ArgumentException("orderBy");
var member = memberExpression.Member;
var memberName = member.Name;
return null;
}
It will throw ArgumentException when orderBy is not simple, member expression.
You can still call the method the same way:
var result = searchEngine.Search(x => x.Name);
Change parameter type of Search to:
Expression<Func<T, K>>
and try like this:
public class SearchEngine<T>
{
// Removed other parameters to make it simple
public IEnumerable<T> Search<K>(Expression<Func<T, K>> orderBy)
{
var name = GetMemberName(orderBy.Body);
throw new NotImplementedException();
}
static string GetMemberName(Expression expression)
{
string memberName = null;
var memberExpression = expression as MemberExpression;
if (memberExpression != null)
memberName = memberExpression.Member.Name;
var unaryExpression = expression as UnaryExpression;
if (unaryExpression != null)
memberName = ((MemberExpression) unaryExpression.Operand).Member.Name;
var methodCallExpression = expression as MethodCallExpression;
if (methodCallExpression != null)
memberName = methodCallExpression.Method.Name;
Contract.Assume(memberName != null);
return memberName;
}
}
You can use Dynamic Linq. The syntax would be something like:
string criteria = "Age < 40 and Age > 30";
string sort = "Name";
var result = searchEngine.Where(criteria).OrderBy(sort);
With databinding objects to controls and grids I hated how the property names would be magic strings, so I created a very simple method as follows:
public static string GetPropertyName<PropertyType>(Expression<Func<T, PropertyType>> expressionForProperty)
{
MemberExpression expression = expressionForProperty.Body as MemberExpression;
return expression.Member.Name;
}
This lets me use code such as:
Product.GetPropertyName(m => m.Name)
to return "Name".
This works perfectly for basic objects. However if I change the method call to be:
Product.GetPropertyName(m => m.ProductCategory.Name)
This also returns "Name". But in order for the databinding to work, I would need it to return "ProductCategory.Name". Is there a way I can get to this by changing the method "GetPropertyName"?
A possible workaround would be to do this:
string test = Product.GetPropertyName(p => p.ProductCategory) + "." + ProductCategory.GetPropertyName(pc => pc.Name)
However, this isn't a neat solution.
This is a modified version of something I might have found here on StackOVerflow:
public static class GetPropertyNameExtension
{
public static string GetPropertyName<TArg, TProperty>(this Expression<Func<TArg, TProperty>> propertyExpression)
{
return propertyExpression.Body.GetMemberExpression().GetPropertyName();
}
public static string GetPropertyName<TProperty>(this Expression<Func<TProperty>> propertyExpression)
{
return propertyExpression.Body.GetMemberExpression().GetPropertyName();
}
public static string GetPropertyName(this MemberExpression memberExpression)
{
if (memberExpression == null)
{
return null;
}
if (memberExpression.Member.MemberType != MemberTypes.Property)
{
return null;
}
var child = memberExpression.Member.Name;
var parent = GetPropertyName(memberExpression.Expression.GetMemberExpression());
return parent == null ?
child
: parent + "." + child;
}
public static MemberExpression GetMemberExpression(this Expression expression)
{
if (expression is MemberExpression)
return (MemberExpression)expression;
if (expression is UnaryExpression)
return (MemberExpression)((UnaryExpression)expression).Operand;
return null;
}
}
I came up with the following which seems to work:
public static string GetComplexPropertyName<PropertyType>(Expression<Func<T, PropertyType>> expressionForProperty)
{
// get the expression body
Expression expressionBody = expressionForProperty.Body as MemberExpression;
string expressionAsString = null;
// all properties bar the root property will be "convert"
switch (expressionBody.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
UnaryExpression unaryExpression = expressionBody as UnaryExpression;
if (unaryExpression != null)
{
expressionAsString = unaryExpression.Operand.ToString();
}
break;
default:
expressionAsString = expressionBody.ToString();
break;
}
// makes ure we have got an expression
if (!String.IsNullOrWhiteSpace(expressionAsString))
{
// we want to get rid of the first operand as it will be the root type, so get the first occurence of "."
int positionOfFirstDot = expressionAsString.IndexOf('.');
if (positionOfFirstDot != -1)
{
return expressionAsString.Substring(positionOfFirstDot + 1, expressionAsString.Length - 1 - positionOfFirstDot);
}
}
return string.Empty;
}
I'm trying to write a strongly typed helper
which would be something like this:
Html.Lookup(x => x.FooId);
for now I have this:
public static MvcHtmlString Lookup<T,TReturn>(this HtmlHelper<T> html, Func<T, TReturn> expression)
{
// get string "FooId" here
}
Anybody knows how to get this ?
public static class ExpressionsExtractor
{
public static string Lookup<T, TProp>(this HtmlHelper<T> html, Expression<Func<T, TProp>> expression)
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
return null;
return memberExpression.Member.Name;
}
}
You would then call it with:
var propName = Html.Lookup(x => x.FooId);
Yet another code.
public MvcHtmlString Lookup<T, TReturn>(this HtmlHelper<T> html, Expression<Func<T, TReturn>> expression)
{
return MvcHtmlString.Create(ExpressionHelper.GetExpressionText(expression));
}
Use ExpressionHelper class.
Func is delegate, Expression is generate ExpressionTree at compile time.
Expression.Compile() return delegate, but Func don't get ExpressionTree at runtime.
Currently using this class when I need this functionality outside of web project where System.Web.Mvc reference shouldn't exist:
namespace Interreg.Domain{
using System;
using System.Linq.Expressions;
public class PropertyName{
public static string For<T>(
Expression<Func<T,object>> expression){
var body=expression.Body;
return GetMemberName(body);
}
public static string For(
Expression<Func<object>> expression){
var body=expression.Body;
return GetMemberName(body);
}
public static string GetMemberName(
Expression expression){
if(expression is MemberExpression){
var memberExpression=(MemberExpression)expression;
if(memberExpression.Expression.NodeType==
ExpressionType.MemberAccess)
return GetMemberName(memberExpression.Expression)+"."+memberExpression.Member.Name;
return memberExpression.Member.Name;
}
if(expression is UnaryExpression){
var unaryExpression=(UnaryExpression)expression;
if(unaryExpression.NodeType!=ExpressionType.Convert)
throw new Exception(string.Format("Cannot interpret member from {0}",expression));
return GetMemberName(unaryExpression.Operand);
}
throw new Exception(string.Format("Could not determine member from {0}",expression));
}
}
}
Good thing about this one is - it does not lose dots when going deeper than just one level.
a bit late but I am posting a simple solution that's working for me in .Net 4. It has handling for value types on line 4
public PropertyInfo GetPropertyInfo<TSource>(Expression<Func<TSource, object>> propertyLambda) {
var member = propertyLambda.Body as MemberExpression;
if (member == null) {// value types return Convert(x.property) which can't be cast to MemberExpression
var expression = propertyLambda.Body as UnaryExpression;
member = expression.Operand as MemberExpression;
}
return member.Member as PropertyInfo;
}
I'm developing a API that uses lambda expressions to specify properties. I'm using this famous piece of code similar to this one (this is simplified and incomplete, just to make clear what I'm talking about):
public void Foo<T, P>(Expression<Func<T, P>> action)
{
var expression = (MemberExpression)action.Body;
string propertyName = expression.Member.Name;
// ...
}
To be called like this:
Foo((String x) => x.Length);
Now I would like to specify a property path by chaining property names, like this:
Foo((MyClass x) => x.Name.Length);
Foo should be able to split the path into its property names ("Name" and "Length"). Is there a way to do this with reasonable effort?
There is a somehow similar looking question, but I think they are trying to combine lambda expressions there.
Another question also is dealing with nested property names, but I don't really understand what they are talking about.
Something like this?
public void Foo<T, P>(Expression<Func<T, P>> expr)
{
MemberExpression me;
switch (expr.Body.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
var ue = expr.Body as UnaryExpression;
me = ((ue != null) ? ue.Operand : null) as MemberExpression;
break;
default:
me = expr.Body as MemberExpression;
break;
}
while (me != null)
{
string propertyName = me.Member.Name;
Type propertyType = me.Type;
Console.WriteLine(propertyName + ": " + propertyType);
me = me.Expression as MemberExpression;
}
}
I played a little with ExpressionVisitor:
public static class PropertyPath<TSource>
{
public static IReadOnlyList<MemberInfo> Get<TResult>(Expression<Func<TSource, TResult>> expression)
{
var visitor = new PropertyVisitor();
visitor.Visit(expression.Body);
visitor.Path.Reverse();
return visitor.Path;
}
private class PropertyVisitor : ExpressionVisitor
{
internal readonly List<MemberInfo> Path = new List<MemberInfo>();
protected override Expression VisitMember(MemberExpression node)
{
if (!(node.Member is PropertyInfo))
{
throw new ArgumentException("The path can only contain properties", nameof(node));
}
this.Path.Add(node.Member);
return base.VisitMember(node);
}
}
}
Usage:
var path = string.Join(".", PropertyPath<string>.Get(x => x.Length).Select(p => p.Name));
Old question, I know... but if it's only the names you need, an even simpler way to do it is:
expr.ToString().Split('.').Skip(1)
EDIT:
public class A
{
public B Property { get; set; }
}
public class B
{
public C field;
}
[Fact]
public void FactMethodName()
{
var exp = (Expression<Func<A, object>>) (x => x.Property.field);
foreach (var part in exp.ToString().Split('.').Skip(1))
Console.WriteLine(part);
// Output:
// Property
// field
}
I have a shared .NET standard DTO between client and server and expressions are a great way to create query strings that can be rebuilt and executed Api side.
A perfect way to create Type safe queries over the wire.
I digress, I needed a property path also
x => x.Siblings.Age
To produce a string like
"Siblings.Age"
I went with this
public static string GetMemberPath(MemberExpression me)
{
var parts = new List<string>();
while (me != null)
{
parts.Add(me.Member.Name);
me = me.Expression as MemberExpression;
}
parts.Reverse();
return string.Join(".", parts);
}
public static string GetPath<T, TProperty>(this Expression<Func<T, TProperty>> exp)
{
return string.Join(".", GetItemsInPath(exp).Reverse());
}
private static IEnumerable<string> GetItemsInPath<T, TProperty>(Expression<Func<T, TProperty>> exp)
{
if (exp == null)
{
yield break;
}
var memberExp = FindMemberExpression(exp.Body);
while (memberExp != null)
{
yield return memberExp.Member.Name;
memberExp = FindMemberExpression(memberExp.Expression);
}
}
I refactored these answers to use recursion, so no need for order reversal. I only need the property tree, so left out all but MemberExpressions. Should be simple to add functionality if need be.
public IEnumerable<string> PropertyPath<TModel, TValue>(
Expression<Func<TModel, TValue>> expression)
{
if (expression.Body is MemberExpression memberExpression)
return PropertyPathRecurse(memberExpression);
return Enumerable.Empty<string>();
}
private IEnumerable<string> PropertyPathRecurse(MemberExpression? expression)
{
if (expression is null)
return Enumerable.Empty<string>();
return PropertyPathRecurse(expression.Expression as MemberExpression)
.Append(expression.Member.Name);
}