Convert string value to operator in C#

30,076

Solution 1

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))

Solution 2

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");
    }
}

Solution 3

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);
        }
    }
Share:
30,076
Eric Herlitz
Author by

Eric Herlitz

System developer, architect and Optimizely MVP that fancies Optimizely/Episerver, Azure, SharePoint and more. Also Co-founder of Bendsoft and the SharePoint .NET Connector. https://twitter.com/EricHerlitz http://www.herlitz.io

Updated on June 19, 2020

Comments

  • Eric Herlitz
    Eric Herlitz almost 4 years

    Im trying to figure out a way to build a conditional dynamically.

    In example

    var greaterThan = ">";
    var a = 1;
    var b = 2;
    
    if(a Convert.ToOperator(greaterThan) b) {...}
    

    I did read this post but could not quite understand how to implement some of the stuff. C# convert a string for use in a logical condition

    Ano advice is highly appreciated

    Thanks

  • dwp4ge
    dwp4ge over 7 years
    Need to handle when left is null