Babu James
Babu James

Reputation: 2843

System.Linq.Dynamic - ParseException: syntax error

I have the following code:

class Program
{
    public class Test
    {
        public string Property { get; set; }
    }
    static void Main(string[] args)
    {
        var expressionString = "Property == \"MySt\\\"ring\"";
        var p = System.Linq.Expressions.Expression.Parameter(typeof(Test));
        var e = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p }, null, expressionString);
    }
}

On executing this, an exception of type ParseException is thrown. The requirement is to have a string literal with a quote in between.

Edit: I have also tried removing \\ from MyString with no change in exception.

Can somebody please share some ideas?

Upvotes: 1

Views: 5169

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

Looks like you can write:

"Property == \"MySt\"\"ring\""

Here's the tokenization code from System.Linq.Dynamic source code:

case '"':
case '\'':
    char quote = ch;
    do {
        NextChar();
        while (textPos < textLen && ch != quote) NextChar();
        if (textPos == textLen)
            throw ParseError(textPos, Res.UnterminatedStringLiteral);
        NextChar();
    } while (ch == quote);
    t = TokenId.StringLiteral;
    break;

Upvotes: 2

Related Questions