title: Parse a Lambda in C#
body: <p>I want to convert a string containing an expression of a known signature <code>Func&lt;dynamic, bool)</code> into a dynamic function I can call.</p>
  
  <pre><code>(x) =&gt; x.Age &gt; 18  //Typecastable to Func&lt;dynamic, bool&gt;
  
  var function = (Func&lt;dynamic, bool&gt;) Lamdba.Parse("(x) =&gt; x.Age &gt; 18")
  </code></pre>
  
  <p>I'm looking for the equivalent of <code>Lambda.Parse</code></p>
  
  <p>Full code of what I'm trying to achieve below:</p>
  
  <pre><code>using System;
  using System.Dynamic;
  using System.Collections.Generic;
  
  public class DynamicModelTests
  {
      public static void Main()
      {
          //Illustration purpose... Working code
          dynamic model = new ExpandoObject();
          model.Age = 18;
          Func&lt;dynamic, bool&gt; isAdultRule = (x) =&gt; x.Age &gt;= 18;
          IDictionary&lt;string, object&gt; dict = model;
          dict["IsAdult"] = (Func&lt;bool&gt;) (() =&gt; isAdultRule(model));
          Console.WriteLine($"Is Adult: {model.IsAdult()}");
          //Working code ends
  
          //Pseudo code starts... Pseudo only because it needs a working Lambda.Parse()
          Compose(model, "IsAdult",  "(x) =&gt; x.Age &gt;= 18");
          Compose(model, "CanWatchRRated", "(x) =&gt; x.IsAdult()");
          bool isAdult = model.CanWatchRRated();
          bool result = ExecuteByName(model, "CanWatchRRated");
          Console.WriteLine($"Is Adult #2: {isAdult}");
          Console.WriteLine($"Same as Is Adult #2: {result}");
          //ends
      }
  
      public static void Compose(dynamic model, string ruleName, string expression)
      {
          var method = GetMethod(model, expression);
          var ruleCallExpression = $"() =&gt; method(model)";
  
          IDictionary&lt;string, object&gt; dict = model;
          dict[ruleName] = (Action) method;
      }
  
      public static Func&lt;dynamic, T&gt; GetMethod&lt;T&gt;(dynamic model, string expression)
      {
          return Lambda.Parse&lt;dynamic, T&gt;(expression); 
      }
  
      public static T Execute&lt;T&gt;(dynamic model, string expression)
      {
          return GetMethod(model, expression)(model);
      }
  
      //Should be called after compose... otherwise, throws MethodNotFound Exception
      public static T ExecuteByName&lt;T&gt;(dynamic model, string ruleName)
      {
          IDictionary&lt;string, object&gt; dictionary = model;
          if(!model.ContainsKey(ruleName))
          {
              throw new MethodNotFoundException();
          }
  
          return (T) model.Invoke(ruleName);
  
      }
  }
  
  public class MethodNotFoundException : Exception
  {
  }
  </code></pre>
  
  <p>Is there a ready-made <code>Lambda.Parse()</code> that I can use?</p>
  
  <p>Note #1:  The model must be of type '<code>dynamic</code>'. I'm using <a href="https://weblog.west-wind.com/posts/2012/feb/08/creating-a-dynamic-extensible-c-expando-object" rel="nofollow noreferrer">Rick Strahl's libary</a> instead of rolling my own <code>DynamicObject</code> implementation</p>
  
  <p>Note #2:  All the examples of <code>System.Linq.Dynamic.DynamicExpression</code> to parse expressions only seem to work with known types.  They didn't work for me with type '<code>dynamic</code>'. If it does for you I'm all ears.  </p>
  
  <p>Thanks!</p>