C# convert a string for use in a logical condition

MrEdmundo picture MrEdmundo · May 29, 2009 · Viewed 19.7k times · Source

Is it possible to convert a string to an operator for use in a logical condition.

For example

if(x Convert.ToOperator(">") y) {}

or

if(x ">" as Operator y){}

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.

Thanks in advance

EDIT: OK I agree, only fair to give some context.

We have system built around reflection and XML. I would like to be able to say something like, for ease.

<Value = "Object1.Value" Operator = ">" Condition = "0"/>

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.

EDIT: Sod it I'm going to have a go.

Imagine the following

<Namespace.LogicRule Value= "Object1.Value" Operator=">" Condition="0">  

This will get reflected into a class, so now I want to test the condition, by calling

bool LogicRule.Test()

That's the bit where it would all need to come together.

EDIT:

OK, so having never looked at Lambdas or Expressions I thought I would have a look after @jrista's suggestions.

My system allows Enums to be parsed, so Expressions are attractive because of the ExpressionType Enum.

So I created the following class to test the idea:

    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<Func<int, int, bool>> expression = Expression.Lambda<Func<int, int, bool>>(
               Expression.MakeBinary(ExpressionType, param, param2), param, param2);

            Func<int, int, bool> del = expression.Compile();

            return del(Convert.ToInt32(Left), Convert.ToInt32(Right));

        }
    }

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.

This way can then be declared in our XML way as

Operation<Left="1" Right="2" ExpressionType="LessThan" Type="System.Int32"/>

Answer

Sean picture Sean · May 29, 2009

You could do something like this:

public static bool Compare<T>(string op, T x, T y) where T:IComparable
{
 switch(op)
 {
  case "==" : return x.CompareTo(y)==0;
  case "!=" : return x.CompareTo(y)!=0;
  case ">"  : return x.CompareTo(y)>0;
  case ">=" : return x.CompareTo(y)>=0;
  case "<"  : return x.CompareTo(y)<0;
  case "<=" : return x.CompareTo(y)<=0;
 }
}