Home >Backend Development >C++ >How Can I Dynamically Evaluate String Expressions Against Objects in C#?

How Can I Dynamically Evaluate String Expressions Against Objects in C#?

DDD
DDDOriginal
2025-01-06 18:01:45171browse

How Can I Dynamically Evaluate String Expressions Against Objects in C#?

Converting String to LINQ Expression Tree for Dynamic Evaluation

Background

In certain scenarios, it becomes necessary to evaluate a logical expression defined as a string against an object instance. For instance, we may have a class representing a person with properties like name, age, and weight, and we want to evaluate a string expression like "Age > 3 AND Weight > 50".

Overengineered Solution Proposal

The initial idea was to employ ANTLR to create an AST from the string expression. The AST would subsequently be traversed to dynamically construct a Func predicate using the Predicate Builder framework. While this approach provides flexibility, it seems overly complex.

Alternative Solution: Dynamic LINQ Library

Instead of developing a custom parser, we can leverage the Dynamic LINQ library, specifically its Dynamic Query class. This class offers a convenient way to parse a string expression and generate a corresponding lambda expression.

Implementation

Here's a code example demonstrating the use of Dynamic LINQ:

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

namespace ExpressionParser
{
  class Program
  {
    public class Person
    {
      public string Name { get; set; }
      public int Age { get; set; }
      public int Weight { get; set; }
      public DateTime FavoriteDay { get; set; }
    }

    static void Main()
    {
      const string filter = "(Person.Age > 3 AND Person.Weight > 50) OR Person.Age < 3";
      var personParameter = Expression.Parameter(typeof(Person), "Person");
      var lambdaExpression = DynamicExpression.ParseLambda(new[] { personParameter }, null, filter);
      var person = new Person
      {
        Name = "Bob",
        Age = 30,
        Weight = 213,
        FavoriteDay = new DateTime(2000, 1, 1)
      };

      bool match = lambdaExpression.Compile().DynamicInvoke(person);
      Console.WriteLine(match);
    }
  }
}

In this example, we parse the expression string into a lambda expression using Dynamic LINQ. We then compile the lambda expression and invoke it on a Person instance to evaluate the expression and obtain a boolean result.

Conclusion

The Dynamic LINQ library provides a more straightforward solution to dynamically evaluating string expressions against objects. It eliminates the need for developing a custom parser and provides a convenient mechanism for constructing lambda expressions from string expressions.

The above is the detailed content of How Can I Dynamically Evaluate String Expressions Against Objects in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn