Convert Expression> to String and vice versa In c#

c# expression expression-trees lambda linq

Question

I want to convert Expression<Func<Loan, bool>> To string and vice versa. can I do it? how can implement ConvertStringToExpression method?

internal class Program
{
    public class Loan
    {
        public bool IsActive { get; set; }
    }

    private static void Main(string[] args)
    {
        Expression<Func<Loan, bool>> expression = l => !l.IsActive;

        var expStr = ConvertExpressionToString(expression);

        var exp = ConvertStringToExpression(expStr);
    }

    public static string ConvertExpressionToString(Expression<Func<Loan, bool>> expression)
    {
        //return ???
        throw new NotImplementedException();
    }

    public static string ConvertStringToExpression(string expression)
    {
        //return ???
        throw new NotImplementedException();
    }
}

Accepted Answer

I continued work on the library that was mentioned by Serializing and Deserializing Expression Trees in C#

It looks like the project was abandoned (2008) but I did some work on it and now it works with .NET 4.0 and Silverlight. I made bug fixes to their code and also made it more DAL-independent.

http://expressiontree.codeplex.com/


Expert Answer

Not in full; however, the Dynamic LINQ sample may help a bit. You can certainly serialize an expression to a string (to an extent - generics look a bit screwy), but there is no inbuilt parser.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow