Eric Herlitz
Eric Herlitz

Reputation: 26277

Convert string value to operator in C#

I'm trying to figure out a way to build a conditional dynamically.

Here is my code so far:

var greaterThan = ">";
var a = 1;
var b = 2;

if (a Convert.ToOperator(greaterThan) b) {...}

I did read this post, but could not figure out how to implement some of the stuff: C# convert a string for use in a logical condition

Any advice is highly appreciated. Thanks.

Upvotes: 20

Views: 39190

Answers (3)

Benbob
Benbob

Reputation: 14254

A more generic way of doing it is to take any IComparable objects.

    public static bool Compare<T>(string op, T left, T right) where T : IComparable<T> {
        switch (op) {
            case "<": return left.CompareTo(right) < 0;
            case ">": return left.CompareTo(right) > 0;
            case "<=": return left.CompareTo(right) <= 0;
            case ">=": return left.CompareTo(right) >= 0;
            case "==": return left.Equals(right);
            case "!=": return !left.Equals(right);
            default: throw new ArgumentException("Invalid comparison operator: {0}", op);
        }
    }

Upvotes: 9

eandersson
eandersson

Reputation: 26352

I wasn't going to post it, but thought that it might be of some help. Assuming of course that you don't need the advanced generic logic in Jon's post.

public static class Extension
{
    public static Boolean Operator(this string logic, int x, int y)
    {
        switch (logic)
        {
            case ">": return x > y;
            case "<": return x < y;
            case "==": return x == y;
            default: throw new Exception("invalid logic");
        }
    }
}

You could use the code like this, with greaterThan being a string with the wanted logic/operator.

if (greaterThan.Operator(a, b))

Upvotes: 26

Jon Skeet
Jon Skeet

Reputation: 1500835

You can't really do that. The closest you could come would be:

Func<T, T, bool> ConvertToBinaryConditionOperator<T>(string op)

and then:

if (ConvertToBinaryConditionOperator<int>(input)(a, b))
{
}

The tricky bit is what ConvertToBinaryConditionOperator would do. You might want to look at Marc Gravell's work on implementing generic operators in MiscUtil. Expression trees could be really useful in this case, although I believe Marc has a working approach which works on .NET 2 as well.

So in this case you might have something like (using MiscUtil)

public static Func<T, T, bool> ConvertToBinaryConditionOperator<T>(string op)
{
    switch (op)
    {
        case "<": return Operator.LessThan<T>;
        case ">": return Operator.GreaterThan<T>;
        case "==": return Operator.Equal<T>;
        case "<=": return Operator.LessThanOrEqual<T>;
        // etc
        default: throw new ArgumentException("op");
    }
}

Upvotes: 16

Related Questions