/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
VS2017
samples/csharp/expression-trees/ExpressionTreeExecutionSampleTwo.cs
61 строка
2 KB
Bill Wagner
Update ci build for ubuntu (#891)
09 авг 2016, 23:02
09 авг 2016, 23:02
ce52ada
Код
Авторство
О чём код?
using System; using System.Linq.Expressions; namespace ExpressionTreeSamples { public class Resource : IDisposable { private bool isDisposed = false; public int Argument { get { if (!isDisposed) return 5; else throw new ObjectDisposedException("Resource"); } } public void Dispose() { isDisposed = true; } } public class ExpressionTreeExecutionSampleTwo : Sample { public override string Name { get; } = "Executing Expression Trees, Sample 2: Bound Variables"; public override void Run() { var del = CreateBoundFunc(); Console.WriteLine(del(5)); try { var del2 = CreateBoundResource(); Console.WriteLine(del2(5)); } catch (ObjectDisposedException e) { Console.WriteLine(e.ToString()); } } private static Func<int, int> CreateBoundFunc() { var constant = 5; // constant is captured by the expression tree Expression<Func<int, int>> expression = (b) => constant + b; var rVal = expression.Compile(); return rVal; } private static Func<int, int> CreateBoundResource() { using (var constant = new Resource()) // constant is captured by the expression tree { Expression<Func<int, int>> expression = (b) => constant.Argument + b; var rVal = expression.Compile(); return rVal; } } } }