C #: Is there a way to use expressions as a parameter / parameter?

I would like to know if an expression can be used as a parameter / parameter in C #. I would like to do something like this:

int x = 0;
public void g()
{
   bool greaterThan = f("x>2");
   bool lessThan = f("x<2");
}
public bool f(Expression expression)
{
   if(expression)
       return true;
   else
       return false;
}

Here is what I do not want to do:

int x = 0;
public void g()
{
    bool greaterThan = f(x, '<', 2);
}

public bool f(int x, char c, int y)
{
    if(c == '<')
       return x < y;
    if(c == '>')
       return x > y;
}

In fact, what I get is a way to get around using a switch or a series of if statements for each of: <> <=> = ==! =. Is there any way to do this?

Edit: Suppose the expression is a string such as "x <2". Is there a way to go from a string to a predicate without using a series of if statements from the condition?

+5
source share
5 answers

This is very possible, just not in the exact syntax you have.

int x = 0;
public void g()
{
   bool greaterThan = f(i => i > 2, x);
   bool lessThan = f(i => i < 2, x);
}
public bool f(Func<int,bool> expression, int value)
{
   return expression(value);
}

, , .

int x = 0;
public void g()
{
   bool greaterThan = f(() => x > 2);
   bool lessThan = f(() => x < 2);
}
public bool f(Func<bool> expression)
{
   return expression();
}

f("x < 2"), . ( ), x, f, .

+11

, Predicate:

int x = 0;
public void g()
{
   bool greaterThan = f(i => i>2, x);
   bool lessThan = f(i => i<2, x);
}
public bool f(Predicate<int> expression, int value)
{
   return expression(value);
}

, bool Expression , :

int x = 0;
public void g()
{
   bool greaterThan = f(x>2);
   bool lessThan = f(x<2);
}
public bool f(bool expression)
{
   if(expression)
       return true;
   else
       return false;
}
+9

- ... :

bool greaterThan = x > 5;
bool lessThan = x < 5;

...

Edit:

, bool:

public void f(bool expression) 
{
    // expression is either true or false...
}

f(x<5); // called like this
+7

: , , ​​ "x < 2". if ?

; , . #, , Microsoft Dynamic LINQ.

:

public void g()
{
    int x = 0;

    bool greaterThan = f("x > 2", x);
    bool lessThan = f("x < 2", x);
}

public bool f(string expression, int x)
{
    ParameterExpression xExpr = Expression.Parameter(typeof(int), "x");

    LambdaExpression e = DynamicExpression.ParseLambda(
        new ParameterExpression[] { xExpr }, typeof(bool), expression);

    return (bool)e.Compile().DynamicInvoke(x);
}

, , . , . , DynamicExpression.ParseLambda LambdaExpression s.

+2

: , , ​​ "x < 2". if ?

There are several tricks that can be used to convert strings to .Net code: CodeDom, Reflection.Emit, or even compiler scripting in the shell. However, none of them are as simple as fast eval()in scripting languages, and in any case, this is usually annoying in .Net if you really don't know what you are doing.

Instead, .Net provides a namespace System.Addinas a more secure way to extend custom extensions for your application.

0
source

All Articles