Created
August 1, 2014 13:21
-
-
Save forcewake/9753e901eea46702a433 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment