Исходный код вики Сценарии

Версия 1.3 от Alexandr Fokin на 2022/06/15 12:57

Последние авторы
1 ==== Компиляция дерева в делегат ====
2
3
4 {{code language="c#"}}
5 Expression
6 .Lambda(expressionValiarble)
7 .Compile();
8 {{/code}}
9
10 ----
11
12 ==== Простое копирование свойств одного класса в другой ====
13
14
15 {{code language="c#"}}
16 //Source
17 ClassA a = new ClassA()
18 {
19 A = 10,
20 B = 20
21 };
22 //Destination
23 ClassB b = new ClassB();
24
25 //Source class instance parameter
26 var parametrB = Expression.Parameter(b.GetType());
27
28 //Destination class instance parameter
29 var parametrA = Expression.Parameter(a.GetType());
30
31 var copyPropertyAEx = Expression.Assign(
32 Expression.Property(parametrB, b.GetType().GetProperty("A")),
33 Expression.Property(parametrA, a.GetType().GetProperty("A"))
34 );
35 var copyPropertyBEx = Expression.Assign(
36 Expression.Property(parametrB, b.GetType().GetProperty("B")),
37 Expression.Property(parametrA, a.GetType().GetProperty("B"))
38 );
39
40 var copyPropertyEx = Expression.Block(
41 copyPropertyAEx,
42 copyPropertyBEx
43 );
44
45 var copyPropertyDelegate = Expression
46 .Lambda<Func<ClassB, ClassA, int>>(copyPropertyEx, parametrB, parametrA)
47 .Compile();
48
49 var result = copyPropertyDelegate.Invoke(b, a);
50 {{/code}}
51
52 ----
53
54 Вызов делегаты при построении EpressionTree
55 Expression Trees and Invoking a Delegate
56 [[https:~~/~~/stackoverflow.com/questions/2215712/expression-trees-and-invoking-a-delegate>>url:https://stackoverflow.com/questions/2215712/expression-trees-and-invoking-a-delegate]]
57
58 ----
59
60 Объявлений локальной переменной в блоке (без передачи извне)
61 [[https:~~/~~/question-it.com/questions/717473/peremennaja-tipa-ukazana-iz-oblasti-vidimosti-no-ne-opredelena>>url:https://question-it.com/questions/717473/peremennaja-tipa-ukazana-iz-oblasti-vidimosti-no-ne-opredelena]]