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();
    }
}
2
0
2/19/2016 7:06:55 PM

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/

48
5/23/2017 12:32:31 PM

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.

2
10/20/2008 10:16:29 AM


Related Questions





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