Paul Cavacas
Paul Cavacas

Reputation: 4454

Turn a property name as a string into a Linq Expression

I need to turn an string that represents a property name on an interface into an expression. I have most of it working, there is just 1 piece that I can't figure out at the end.

 static Expression<Func<T, int>> MakeGetter<T>(string propertyName)
    {
         var input = Expression.Parameter(typeof(T));
         var property = typeof(T).GetProperty(propertyName) ??
                                   GetProperty(propertyName, typeof(T));

         var expr = Expression.Property(input, property);
         var propType = property.PropertyType.Name;

         switch (propType.ToLower())
         {
              case "string":
                   return Expression.Lambda<Func<T, string>>(expr, input);
              case "int":
                   return Expression.Lambda<Func<T, int>>(expr, input);
         }
  }

  private static PropertyInfo GetProperty(string propertyName, Type i)
  {
       var baseInterfaces = i.GetInterfaces();
       foreach (var baseInterface in baseInterfaces)
       {
            var property = baseInterface.GetProperty(propertyName);
            return property ?? GetProperty(propertyName, baseInterface);
       }
       return null;
  }

The one problem that I have is at the end of the MakeGetter function I don't know if the function is a string or int or some other type and have no way of knowing until after I do all of the reflection, so how can I create this method, so that it is generic and will return an Expression correctly.

Upvotes: 1

Views: 1807

Answers (2)

Martin Staufcik
Martin Staufcik

Reputation: 9490

Have a look here at an example of generating lambda expressions at runtime. These lines from the article show how to generate lambda expression:

var parameterExpression = Expression.Parameter(typeof(TEntity), "x");
var memberExpression = Expression.PropertyOrField(parameterExpression, prop.Name);
var memberExpressionConversion = Expression.Convert(memberExpression, typeof(object));
var lambda = Expression.Lambda<Func<TEntity, object>>(memberExpressionConversion, parameterExpression);

Upvotes: 2

Ocelot20
Ocelot20

Reputation: 10800

Now that you've declared your intent for this in the comments, I believe that the accepted answer here is what you're looking for. Even if it's not doing exactly what you're looking for, these lines can carry over:

// TODO: Get 'type' from the property you want.
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

You could return LambdaExpression, or just use the extension methods found in that answer, or use DynamicLinq. I guess the moral of the story (as it often is) is that its probably been done already :).

Upvotes: 0

Related Questions