2011-08-01 36 views

回答

5

我懷疑你可以使用Expression.Convert

Expression<Func<MyClass, int>> original = ...; 

var conversion = Expression.Lambda<Func<MyClass, object>>(
     Expression.Convert(original.Body, typeof(object)), 
     original.Parameters); 

var conversionBack = Expression.Lambda<Func<MyClass, int>>(
     Expression.Convert(conversion.Body, typeof(int)), 
     original.Parameters); 

編輯:好的,它看起來它的工作原理:

using System; 
using System.Linq; 
using System.Linq.Expressions; 

class Test 
{ 
    static void Main() 
    { 
     Expression<Func<string, int>> original = x => x.Length; 
     var conversion = Expression.Lambda<Func<string, object>(
       Expression.Convert(original.Body, typeof(object)), 
       original.Parameters); 

     var conversionBack = Expression.Lambda<Func<string, int>>(
       Expression.Convert(conversion.Body, typeof(int)), 
       original.Parameters); 

     Console.WriteLine(conversion); // x => Convert(x.Length) 
     Console.WriteLine(conversionBack); // x => Convert(Convert(x.Length)) 

     Console.WriteLine(conversion.Compile()("Hello")); // 5 
     Console.WriteLine(conversionBack.Compile()("Hello")); // 5 
    } 
} 
相關問題