Csharp/CSharp Tutorial/LINQ/Expressions — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:18, 26 мая 2010

Cast to BinaryExpression

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Xml.Linq;
class Program
{
    static void Main(string[] args)
    {
        Func<int, int> func1 = x => x + 5;
        var result = func1(1);
        Expression<Func<int, int>> expression = func => func + 5;
        var originalDelegate = expression.rupile();
        var three = originalDelegate.Invoke(2);
        BinaryExpression operation = (BinaryExpression)expression.Body;
        Console.WriteLine(operation.NodeType);
    }
}

Cast to ParameterExpression

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Xml.Linq;
class Program
{
    static void Main(string[] args)
    {
        Func<int, int> func1 = x => x + 5;
        var result = func1(1);
        Expression<Func<int, int>> expression = func => func + 5;
        var originalDelegate = expression.rupile();
        var three = originalDelegate.Invoke(2);
        ParameterExpression parameter = (ParameterExpression)expression.Parameters[0];
        Console.WriteLine(parameter.Name);
    }
}

Compiled Expression Tree

using System;
using System.ruponentModel;
using System.Linq.Expressions;
    class CompiledExpressionTree
    {
        static void Main()
        {
            Expression firstArg = Expression.Constant(2);
            Expression secondArg = Expression.Constant(3);
            Expression add = Expression.Add(firstArg, secondArg);
            Func<int> compiled = Expression.Lambda<Func<int>>(add).rupile();
            Console.WriteLine(compiled());
        }
    }

Expression Tree

using System;
using System.Linq.Expressions;
using System.ruponentModel;
    class MainClass
    {
        static void Main()
        {
            Expression firstArg = Expression.Constant(2);
            Expression secondArg = Expression.Constant(3);
            Expression add = Expression.Add(firstArg, secondArg);
            Console.WriteLine(add);
        }
    }

Lambda Expression To Expression Tree

using System;
using System.ruponentModel;
using System.Linq.Expressions;
    class MainClass
    {
        static void Main()
        {
            Expression<Func<int>> return5 = () => 5;
            Func<int> compiled = return5.rupile();
            Console.WriteLine(compiled());
        }
    }

Lambda Expression With Parameters To Expression Tree

using System;
using System.ruponentModel;
using System.Linq.Expressions;
    class MainClass
    {
        static void Main()
        {
            Expression<Func<string, string, bool>> expression = ((x, y) => x.StartsWith(y));
            var compiled = expression.rupile();
            Console.WriteLine(compiled("First", "Second"));
            Console.WriteLine(compiled("First", "Fir"));
        }
    }