I am building a Switch expression to match ranges of integers at runtime.
Currently I am able to compile and run the equivalent of the following using Expression.SwitchCase
:
switch(value)
{
case 1:
case 2:
Console.WriteLine("1 or 2");
break;
case 3:
case 4:
case 5:
Console.WriteLine("3, 4 or 5");
break;
}
My issue being - I have to create a SwitchCase for every constant in the range I wish to match:
Expression.SwitchCase(body, Expression.Constant(1))
Expression.SwitchCase(body, Expression.Constant(2))
Is there a more concise way to achieve this? Is there a way I can replace that constant expression with an expression that evaluates the switched value against a range?. Performance is also of interest, especially if the range is large.