Изменения документа Сценарии

Редактировал(а) Alexandr Fokin 2025/05/13 13:04

От версии 2.10
отредактировано Alexandr Fokin
на 2025/05/13 13:01
Изменить комментарий: К данной версии нет комментариев
К версии 2.5
отредактировано Alexandr Fokin
на 2023/05/31 09:59
Изменить комментарий: К данной версии нет комментариев

Сводка

Подробности

Свойства страницы
Содержимое
... ... @@ -13,8 +13,14 @@
13 13  |(% style="width:394px" %) |(% style="width:1090px" %)(((
14 14  How do I set a field value in an C# Expression tree?
15 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 +How do you call a constructor via an expression tree on an existing object?
18 +[[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]]
19 +
20 +How do I set a field value in an C# Expression tree?
21 +[[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 16  )))
17 -|(% style="width:394px" %)Типизированый вызов конструктора|(% style="width:1090px" %){{code language="c#"}}var constructorInfo = type.GetConstructors().First(e => e.Attributes.HasFlag(MethodAttributes.Public));
23 +|(% style="width:394px" %)Вызов конструктора|(% style="width:1090px" %){{code language="c#"}}var constructorInfo = t.GetConstructors().First(e => e.Attributes.HasFlag(MethodAttributes.Public));
18 18  var constructorParameter1 = Expression.Parameter(typeof(bool));
19 19  var constructorExp = Expression.New(constructorInfo, constructorParameter1);
20 20  
... ... @@ -28,111 +28,20 @@
28 28  How do you call a constructor via an expression tree on an existing object?
29 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 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?[]));
38 +==== ====
38 38  
39 -Expression[] parameterExpressions;
40 -{
41 - ParameterInfo[] parametersInfos = constructorInfo.GetParameters();
42 - parameterExpressions = new Expression[parametersInfos.Length];
40 +==== ====
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 -}
42 +==== ====
57 57  
58 -NewExpression constructorCallExp = Expression.New(
59 - constructorInfo,
60 - parameterExpressions);
61 61  
62 -var convertResultExp = Expression.Convert(
63 - constructorCallExp,
64 - typeof(object)
65 - );
45 +----
66 66  
67 -var constructorHandler = Expression
68 - .Lambda<ConstructorDelegate>(
69 - convertResultExp,
70 - constructorParameters
71 - )
72 - .Compile();
47 +==== ====
73 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 78  
79 -public static MethodDelegate GetMethodWithArgsDelegate(
80 - MethodInfo methodInfo,
81 - Type instanceType)
82 -{
83 -ParameterExpression instanceParameter = Expression.Parameter(typeof(object));
84 -ParameterExpression methodParameters = Expression.Parameter(typeof(object?[]));
50 +----
85 85  
86 -Expression instanceExp;
87 -Expression[] parameterExpressions;
88 -{
89 - instanceExp = Expression.Convert(instanceParameter, p.instanceType);
90 90  
91 - ParameterInfo[] parametersInfos = p.methodInfo.GetParameters();
92 - parameterExpressions = new Expression[parametersInfos.Length];
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
112 - );
113 -
114 -var convertResultExp = Expression.Convert(
115 - callExpression,
116 - typeof(object)
117 - );
118 -
119 -var callHandler = Expression
120 - .Lambda<MethodDelegate>(
121 - convertResultExp,
122 - instanceParameter,
123 - methodParameters
124 - )
125 - .Compile();
126 -
127 -return callHandler;
128 -}{{/code}}|
129 -
130 -
131 -)))
132 -|(% style="width:394px" %)[[Generic enum to int>>doc:.Generic enum to int.WebHome]]|(% style="width:1090px" %)
133 -
134 -==== ====
135 -
136 -==== ====
137 -
138 -==== ====
53 +----