PageRenderTime 36ms CodeModel.GetById 32ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Runtime/Samples/ExpressionTree/UESamples/ConsoleApplication1/CIfThen.cs

#
C# | 54 lines | 38 code | 8 blank | 8 comment | 0 complexity | b768202558d5c05c104bbbe9439196f0 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using Microsoft.Scripting.Ast;
 5
 6namespace Samples {
 7    class CIfThen {
 8        //Expression.IfThen(MethodInfo, Expression[])
 9        static public void IfThen1() {
10            //<Snippet1>
11            // add the following directive to your file
12            // using Microsoft.Scripting.Ast;  
13
14            //We'll define a helper lambda to print out some information
15            ParameterExpression Text = Expression.Parameter(typeof(string));
16            var MyPrint = Expression.Lambda<Action<string>>(
17                Expression.Call(
18                    null,
19                    typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)}),
20                    Text
21                ),
22                Text
23            );
24
25            //This element defines an IfThen expression.
26            Expression MyIfThen1 = Expression.IfThen(
27                Expression.Constant(true),
28                Expression.Invoke(
29                    MyPrint,
30                    Expression.Constant("Ran 1")
31                )
32            );
33
34            Expression MyIfThen2 = Expression.IfThen(
35                Expression.Constant(false),
36                Expression.Invoke(
37                    MyPrint,
38                    Expression.Constant("Ran 2")
39                )
40            );
41
42            var MyBlock = Expression.Block(
43                MyIfThen1,
44                MyIfThen2
45            );
46
47            //The end result should be "Ran 1".
48            Expression.Lambda<Action>(MyBlock).Compile().Invoke();
49            //</Snippet1>
50        }
51
52
53    }
54}