title: C# convert a string for use in a logical condition
body: <p>Is it possible to convert a string to an operator for use in a logical condition.</p>
  
  <p>For example</p>
  
  <pre><code>if(x Convert.ToOperator("&gt;") y) {}
  </code></pre>
  
  <p>or</p>
  
  <pre><code>if(x "&gt;" as Operator y){}
  </code></pre>
  
  <p>I appreciate that this might not be standard practice question, therefore I'm not interested in answers that ask me why the hell would want to do something like this.</p>
  
  <p>Thanks in advance</p>
  
  <p>EDIT: OK I agree, only fair to give some context.</p>
  
  <p>We have system built around reflection and XML. I would like to be able to say something like, for ease. </p>
  
  <pre><code>&lt;Value = "Object1.Value" Operator = "&gt;" Condition = "0"/&gt;
  </code></pre>
  
  <p>EDIT: Thanks for your comments, I can't properly explain this on here. I guess my question is answered by "You can't", which is absolutely fine (and what I thought). Thanks for your comments.</p>
  
  <p>EDIT: Sod it I'm going to have a go.</p>
  
  <p>Imagine the following</p>
  
  <pre><code>&lt;Namespace.LogicRule Value= "Object1.Value" Operator="&gt;" Condition="0"&gt;  
  </code></pre>
  
  <p>This will get reflected into a class, so now I want to test the condition, by calling</p>
  
  <pre><code>bool LogicRule.Test()
  </code></pre>
  
  <p>That's the bit where it would all need to come together.</p>
  
  <p>EDIT:</p>
  
  <p>OK, so having never looked at Lambdas or Expressions I thought I would have a look after @jrista's suggestions.</p>
  
  <p>My system allows Enums to be parsed, so Expressions are attractive because of the ExpressionType Enum. </p>
  
  <p>So I created the following class to test the idea:</p>
  
  <pre><code>    public class Operation
      {
          private object _Right;
          private object _Left;
          private ExpressionType _ExpressionType;
          private string _Type;
  
          public object Left
          {
              get { return _Left; }
              set { _Left = value; }
          }
  
          public object Right
          {
              get { return _Right; }
              set { _Right = value; }
          }
  
          public string Type
          {
              get { return _Type; }
              set { _Type = value; }
          }
  
          public ExpressionType ExpressionType
          {
              get { return _ExpressionType; }
              set { _ExpressionType = value; }
          }
  
          public bool Evaluate()
          {
              var param = Expression.Parameter(typeof(int), "left");
              var param2 = Expression.Parameter(typeof(int), "right");
  
              Expression&lt;Func&lt;int, int, bool&gt;&gt; expression = Expression.Lambda&lt;Func&lt;int, int, bool&gt;&gt;(
                 Expression.MakeBinary(ExpressionType, param, param2), param, param2);
  
              Func&lt;int, int, bool&gt; del = expression.Compile();
  
              return del(Convert.ToInt32(Left), Convert.ToInt32(Right));
  
          }
      }
  </code></pre>
  
  <p>Obviously this will only work for Int32 right now and the basic ExpressionTypes, I'm not sure I can make it generic? I've never use Expressions before, however this seems to work.</p>
  
  <p>This way can then be declared in our XML way as</p>
  
  <pre><code>Operation&lt;Left="1" Right="2" ExpressionType="LessThan" Type="System.Int32"/&gt;
  </code></pre>