Изменения документа Сценарии
Редактировал(а) Alexandr Fokin 2025/05/13 13:04
От версии 2.10
отредактировано Alexandr Fokin
на 2025/05/13 13:01
на 2025/05/13 13:01
Изменить комментарий:
К данной версии нет комментариев
К версии 1.3
отредактировано Alexandr Fokin
на 2022/06/15 12:57
на 2022/06/15 12:57
Изменить комментарий:
К данной версии нет комментариев
Сводка
-
Свойства страницы (2 изменено, 0 добавлено, 0 удалено)
Подробности
- Свойства страницы
-
- Родительский документ
-
... ... @@ -1,1 +1,1 @@ 1 -Разработка.NET.C#.Рантайм и типы.Expression tree | Деревья выражений.WebHome1 +Разработка.NET.C#.Рантайм и типы.Expression tree.WebHome - Содержимое
-
... ... @@ -1,138 +1,61 @@ 1 -|(% style="width:394px" %)Компиляция дерева в делегат|(% style="width:1090px" %){{code language="c#"}}Expression 2 - .Lambda(expressionValiarble) 3 - .Compile();{{/code}} 4 -|(% style="width:394px" %)[[Простое копирование свойств одного класса в другой>>doc:Разработка.NET.C#.Рантайм и типы.Expression tree | Деревья выражений.Сценарии.Простое копирование свойств одного класса в другой.WebHome]]|(% style="width:1090px" %) 5 -|(% style="width:394px" %)[[Маппинг на основе выражения доступа к свойству>>doc:.Маппинг на основе выражения доступа к свойству.WebHome]]|(% style="width:1090px" %) 6 -|(% style="width:394px" %)Вызов делегата при построении EpressionTree|(% style="width:1090px" %)Expression Trees and Invoking a Delegate 7 -[[https:~~/~~/stackoverflow.com/questions/2215712/expression-trees-and-invoking-a-delegate>>url:https://stackoverflow.com/questions/2215712/expression-trees-and-invoking-a-delegate]] 8 -|(% style="width:394px" %)Объявлений локальной переменной в блоке 9 -(без передачи извне)|(% style="width:1090px" %)((( 10 -переменная типа указана из области видимости, но не определена 11 -[[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]] 12 -))) 13 -|(% style="width:394px" %) |(% style="width:1090px" %)((( 14 -How do I set a field value in an C# Expression tree? 15 -[[https:~~/~~/stackoverflow.com/questions/321650/how-do-i-set-a-field-value-in-an-c-sharp-expression-tree>>https://stackoverflow.com/questions/321650/how-do-i-set-a-field-value-in-an-c-sharp-expression-tree]] 16 -))) 17 -|(% style="width:394px" %)Типизированый вызов конструктора|(% style="width:1090px" %){{code language="c#"}}var constructorInfo = type.GetConstructors().First(e => e.Attributes.HasFlag(MethodAttributes.Public)); 18 -var constructorParameter1 = Expression.Parameter(typeof(bool)); 19 -var constructorExp = Expression.New(constructorInfo, constructorParameter1); 1 +==== Компиляция дерева в делегат ==== 20 20 21 -// Func<bool, object> or Func<bool, T> 22 -var constructorDelegate = Expression 23 - .Lambda<Func<bool, object>>( 24 - constructorExp, 25 - constructorParameter1 26 - ) 27 - .Compile();{{/code}}((( 28 -How do you call a constructor via an expression tree on an existing object? 29 -[[https:~~/~~/stackoverflow.com/questions/16363838/how-do-you-call-a-constructor-via-an-expression-tree-on-an-existing-object>>https://stackoverflow.com/questions/16363838/how-do-you-call-a-constructor-via-an-expression-tree-on-an-existing-object]] 30 -))) 31 -|(% style="width:394px" %)Нетипизированый вызов|(% style="width:1090px" %)((( 32 -|{{code language="c#"}}public delegate object ConstructorDelegate(object[]? Args); 33 33 34 -public static ConstructorDelegate GetConstructorWithArgsDelegate( 35 - ConstructorInfo constructorInfo) 36 -{ 37 -ParameterExpression constructorParameters = Expression.Parameter(typeof(object?[])); 4 +{{code language="c#"}} 5 +Expression 6 + .Lambda(expressionValiarble) 7 + .Compile(); 8 +{{/code}} 38 38 39 -Expression[] parameterExpressions; 40 -{ 41 - ParameterInfo[] parametersInfos = constructorInfo.GetParameters(); 42 - parameterExpressions = new Expression[parametersInfos.Length]; 10 +---- 43 43 44 - for (int i = 0; i < parametersInfos.Length; i++) 45 - { 46 - ConstantExpression ithIndex = Expression.Constant(i); 47 - BinaryExpression ithParameter = Expression.ArrayIndex( 48 - constructorParameters, 49 - ithIndex); 50 - UnaryExpression unboxedIthParameter = Expression.Convert( 51 - ithParameter, 52 - parametersInfos[i].ParameterType 53 - ); 54 - parameterExpressions[i] = unboxedIthParameter; 55 - } 56 -} 12 +==== Простое копирование свойств одного класса в другой ==== 57 57 58 -NewExpression constructorCallExp = Expression.New( 59 - constructorInfo, 60 - parameterExpressions); 61 61 62 -var convertResultExp = Expression.Convert( 63 - constructorCallExp, 64 - typeof(object) 65 - ); 66 - 67 -var constructorHandler = Expression 68 - .Lambda<ConstructorDelegate>( 69 - convertResultExp, 70 - constructorParameters 71 - ) 72 - .Compile(); 73 - 74 -return constructorHandler; 75 -}{{/code}}|C# Expression tree: object array to Expression.New() parameters 76 -[[https:~~/~~/stackoverflow.com/questions/67968927/c-sharp-expression-tree-object-array-to-expression-new-parameters>>https://stackoverflow.com/questions/67968927/c-sharp-expression-tree-object-array-to-expression-new-parameters]] 77 -|{{code language="c#"}}public delegate object? MethodDelegate(object instance, object[]? Args); 78 - 79 -public static MethodDelegate GetMethodWithArgsDelegate( 80 - MethodInfo methodInfo, 81 - Type instanceType) 15 +{{code language="c#"}} 16 +//Source 17 +ClassA a = new ClassA() 82 82 { 83 -ParameterExpression instanceParameter = Expression.Parameter(typeof(object)); 84 -ParameterExpression methodParameters = Expression.Parameter(typeof(object?[])); 19 + A = 10, 20 + B = 20 21 +}; 22 +//Destination 23 +ClassB b = new ClassB(); 85 85 86 -Expression instanceExp; 87 -Expression[] parameterExpressions; 88 -{ 89 - instanceExp = Expression.Convert(instanceParameter, p.instanceType); 25 +//Source class instance parameter 26 +var parametrB = Expression.Parameter(b.GetType()); 90 90 91 - ParameterInfo[]parametersInfos= p.methodInfo.GetParameters();92 - parameterExpressions=newExpression[parametersInfos.Length];28 +//Destination class instance parameter 29 +var parametrA = Expression.Parameter(a.GetType()); 93 93 94 - for (int i = 0; i < parametersInfos.Length; i++) 95 - { 96 - ConstantExpression ithIndex = Expression.Constant(i); 97 - BinaryExpression ithParameter = Expression.ArrayIndex( 98 - methodParameters, 99 - ithIndex); 100 - UnaryExpression unboxedIthParameter = Expression.Convert( 101 - ithParameter, 102 - parametersInfos[i].ParameterType 103 - ); 104 - parameterExpressions[i] = unboxedIthParameter; 105 - } 106 -} 107 - 108 -var callExpression = Expression.Call( 109 - instanceExp, 110 - p.methodInfo, 111 - parameterExpressions 31 +var copyPropertyAEx = Expression.Assign( 32 + Expression.Property(parametrB, b.GetType().GetProperty("A")), 33 + Expression.Property(parametrA, a.GetType().GetProperty("A")) 112 112 ); 113 - 114 -var convertResultExp = Expression.Convert( 115 - callExpression, 116 - typeof(object) 35 +var copyPropertyBEx = Expression.Assign( 36 + Expression.Property(parametrB, b.GetType().GetProperty("B")), 37 + Expression.Property(parametrA, a.GetType().GetProperty("B")) 117 117 ); 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(); 118 118 119 -var callHandler = Expression 120 - .Lambda<MethodDelegate>( 121 - convertResultExp, 122 - instanceParameter, 123 - methodParameters 124 - ) 125 - .Compile(); 49 +var result = copyPropertyDelegate.Invoke(b, a); 50 +{{/code}} 126 126 127 -return callHandler; 128 -}{{/code}}| 52 +---- 129 129 130 - 131 - )))132 - |(%style="width:394px" %)[[Genericenumtoint>>doc:.Genericenumtoint.WebHome]]|(%style="width:1090px" %)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]] 133 133 134 - ==== ====58 +---- 135 135 136 -==== ==== 137 - 138 -==== ==== 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]]