What does it mean for a label target to get a value?

I have a few questions about System.Linq.Expressions.LabelExpressionboth its helper classes and methods.

1) The documentation for the class LabelExpressionreads this way:

Represents a label that can be placed in any context of an expression. If he jumped, he would get a value corresponding to the corresponding one GotoExpression. Otherwise, it gets the value in DefaultValue. If Typeequal System.Void, the value shall not be specified.

What does it mean to return the value to the target label? In other words, what does it mean that the target of the label should receive a value? I've never done this in my life - pass the value to the target label when I go to it?

2) Although it’s completely advisable to go to the target label, what does it mean to go back and continue and interrupt the goal of the label?

+4
source share
2 answers

Sometimes it's useful to think of Linq expressions as a way to create code in something that resembles C # but is not exactly C #. This is just the case.

The code below is an implementation Math.Max(int a, int b)using expressions. For operators returnlike C #, there is no shortcut. You must create tags.

        // (a, b => 
        // {
        //      if(a > b)
        //          return a;
        //      else
        //          return b;
        // }

        var a = Expression.Parameter(typeof(int), "a");
        var b = Expression.Parameter(typeof(int), "b");
        var returnLabel = Expression.Label(typeof (int));
        Expression<Func<int, int, int>> returnMax = (Expression<Func<int, int, int>>)Expression.Lambda
            (
                Expression.Block
                (
                    Expression.IfThenElse
                    (
                        Expression.GreaterThan(a, b),
                        Expression.Return(returnLabel, a),
                        Expression.Return(returnLabel, b)
                    ),
                    Expression.Label(returnLabel, Expression.Constant(0))
                ),
                a,
                b
            );
        var shouldBeSix = returnMax.Compile()(5, 6);

, LabelExpression : ( void - ) . A BlockExpression, , . AssignExpression . , a LabelExpression . GotoExpression , :

        var returnLabel = Expression.Label(typeof (int));
        Expression<Func<int>> returnsSix = (Expression<Func<int>>)Expression.Lambda
            (
                Expression.Label(
                    returnLabel, 
                    Expression.Constant(6)
                )
            );

        var alsoSix = returnsSix.Compile()();

... , .

a LabelExpression , LabelTarget GotoExpression. 0 , , , . 0 0.0 null, .Compile().

2) , "" . @Grax, Expression.Goto, Expression.Continue, Expression.Break, Expression.Return GotoExpressions, .

+4

. , " " "target" . Expression.Return Expression.Break . Expression.Continue Label.

var target = Expression.Label(typeof(string));
var debugPrint = typeof(Debug).GetMethod("Print", new Type[] { typeof(string) });

var expr = Expression.Block(typeof(string),
    new Expression[] {
        Expression.Call(debugPrint,Expression.Constant("Before")),
        Expression.Return(target,Expression.Constant("payload"),typeof(string)),
        //Expression.Break(target,Expression.Constant("payload")),
        Expression.Call(debugPrint,Expression.Constant("During")),
        Expression.Label(target,Expression.Constant("Default")),
    }
);

var result = Expression.Lambda<Func<string>>(expr).Compile()();

.

string Demo()
{
    Debug.Print("Before");
    return "payload";
    Debug.Print("During");
    return "Default";
}

: "" . "", "" "" - , "" . , , . "" "" , .

+1

Source: https://habr.com/ru/post/1567992/


All Articles