如何投射表达式<func <t,datetime =“”>&gt;表达式<func <t,object =“”>&gt; </func <t,> </func <t,>

时间:2009-04-08 09:50:31

标签: c# lambda

我一直在搜索,但我找不到如何从类型

中进行投射
Expression<Func<T, DateTime>>

到类型:

Expression<Func<T, object>>

所以我必须再次转向大量的知识;)

4 个答案:

答案 0 :(得分:71)

你不能只是在他们之间施展,因为他们不是同一种东西。但是,您可以在表达式树中有效地添加转换:

using System;
using System.Linq.Expressions;

class Test
{
    // This is the method you want, I think
    static Expression<Func<TInput,object>> AddBox<TInput, TOutput>
        (Expression<Func<TInput, TOutput>> expression)
    {
        // Add the boxing operation, but get a weakly typed expression
        Expression converted = Expression.Convert
             (expression.Body, typeof(object));
        // Use Expression.Lambda to get back to strong typing
        return Expression.Lambda<Func<TInput,object>>
             (converted, expression.Parameters);
    }

    // Just a simple demo
    static void Main()
    {
        Expression<Func<string, DateTime>> x = text => DateTime.Now;
        var y = AddBox(x);        
        object dt = y.Compile()("hi");
        Console.WriteLine(dt);
    }        
}

答案 1 :(得分:24)

Rob Jon Skeet 的答案有一个问题。

你得到像x => Convert(x.PropertyName)这样的东西,但是例如对于 ASP.NET MVC ,你想要一个这样的表达式x => x.PropertyName

所以Expression.Convert &#34;污染&#34; 表达某些情况。

<强>解决方案:

public static class LambdaExpressionExtensions
{
    public static Expression<Func<TInput, object>> ToUntypedPropertyExpression<TInput, TOutput> (this Expression<Func<TInput, TOutput>> expression)
    {
        var memberName = ((MemberExpression)expression.Body).Member.Name;

        var param = Expression.Parameter(typeof(TInput));
        var field = Expression.Property(param, memberName);
        return Expression.Lambda<Func<TInput, object>>(field, param);
    }
}

<强>用法:

Expression<Func<T, DateTime>> expression = ...;
Expression<Func<T, object>> expr = expression.ToUntypedPropertyExpression();

答案 2 :(得分:10)

根据Jon的代码(感谢顺便说一句),您可以更进一步,以获得完全的灵活性:

public static Expression<Func<TModel, TToProperty>> Cast<TModel, TFromProperty, TToProperty>(Expression<Func<TModel, TFromProperty>> expression)
{
    Expression converted = Expression.Convert(expression.Body, typeof(TToProperty));

    return Expression.Lambda<Func<TModel, TToProperty>>(converted, expression.Parameters);
}

答案 3 :(得分:0)

只需将TResult定义为对象并编译表达式,它适用于所有数据类型;

Expression<Func<string, object>> dateExp = text => DateTime.Now;
object dt = dateExp.Compile()("hi");
Console.WriteLine(dt);

Fiddle sample here