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

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

От версии 2.4
отредактировано Alexandr Fokin
на 2023/05/31 09:57
Изменить комментарий: К данной версии нет комментариев
К версии 2.12
отредактировано Alexandr Fokin
на 2025/05/13 13:04
Изменить комментарий: К данной версии нет комментариев

Сводка

Подробности

Свойства страницы
Содержимое
... ... @@ -13,14 +13,8 @@
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]]
22 22  )))
23 -|(% style="width:394px" %)Вызов конструктора|(% style="width:1090px" %){{code language="c#"}}var constructorInfo = t.GetConstructors().First(e => e.Attributes.HasFlag(MethodAttributes.Public));
17 +|(% style="width:394px" %)Типизированый вызов конструктора|(% style="width:1090px" %){{code language="c#"}}var constructorInfo = type.GetConstructors().First(e => e.Attributes.HasFlag(MethodAttributes.Public));
24 24  var constructorParameter1 = Expression.Parameter(typeof(bool));
25 25  var constructorExp = Expression.New(constructorInfo, constructorParameter1);
26 26  
... ... @@ -28,26 +28,119 @@
28 28  var constructorDelegate = Expression
29 29   .Lambda<Func<bool, object>>(
30 30   constructorExp,
31 - deletedParameter
25 + constructorParameter1
32 32   )
33 33   .Compile();{{/code}}(((
34 34  How do you call a constructor via an expression tree on an existing object?
35 35  [[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]]
36 36  )))
31 +|(% style="width:394px" %)Нетипизированый вызов|(% style="width:1090px" %)(((
32 +|{{code language="c#"}}public delegate object ConstructorDelegate(object[]? Args);
37 37  
38 -==== ====
34 +public static ConstructorDelegate GetConstructorWithArgsDelegate(
35 + ConstructorInfo constructorInfo)
36 +{
37 + ParameterExpression constructorParameters = Expression.Parameter(typeof(object?[]));
38 +
39 + Expression[] parameterExpressions;
40 + {
41 + ParameterInfo[] parametersInfos = constructorInfo.GetParameters();
42 + parameterExpressions = new Expression[parametersInfos.Length];
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 + }
57 +
58 + NewExpression constructorCallExp = Expression.New(
59 + constructorInfo,
60 + parameterExpressions);
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}}|{{code language="c#"}}object Func<object[]>{{/code}}(((
76 +C# Expression tree: object array to Expression.New() parameters
77 +[[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]]
78 +)))
79 +|{{code language="c#"}}public delegate object? MethodDelegate(object instance, object[]? Args);
39 39  
40 -==== ====
81 +public static MethodDelegate GetMethodWithArgsDelegate(
82 + MethodInfo methodInfo,
83 + Type instanceType)
84 +{
85 + ParameterExpression instanceParameter = Expression.Parameter(typeof(object));
86 + ParameterExpression methodParameters = Expression.Parameter(typeof(object?[]));
87 +
88 + Expression instanceExp;
89 + Expression[] parameterExpressions;
90 + {
91 + instanceExp = Expression.Convert(instanceParameter, instanceType);
92 +
93 + ParameterInfo[] parametersInfos = methodInfo.GetParameters();
94 + parameterExpressions = new Expression[parametersInfos.Length];
95 +
96 + for (int i = 0; i < parametersInfos.Length; i++)
97 + {
98 + ConstantExpression ithIndex = Expression.Constant(i);
99 + BinaryExpression ithParameter = Expression.ArrayIndex(
100 + methodParameters,
101 + ithIndex);
102 + UnaryExpression unboxedIthParameter = Expression.Convert(
103 + ithParameter,
104 + parametersInfos[i].ParameterType
105 + );
106 + parameterExpressions[i] = unboxedIthParameter;
107 + }
108 + }
109 +
110 + var callExpression = Expression.Call(
111 + instanceExp,
112 + methodInfo,
113 + parameterExpressions
114 + );
115 +
116 + var convertResultExp = Expression.Convert(
117 + callExpression,
118 + typeof(object)
119 + );
120 +
121 + var callHandler = Expression
122 + .Lambda<MethodDelegate>(
123 + convertResultExp,
124 + instanceParameter,
125 + methodParameters
126 + )
127 + .Compile();
128 +
129 + return callHandler;
130 +}{{/code}}|{{code language="c#"}}object? Func<object, object[]>{{/code}}
41 41  
132 +
133 +)))
134 +|(% style="width:394px" %)[[Generic enum to int>>doc:.Generic enum to int.WebHome]]|(% style="width:1090px" %)
135 +
42 42  ==== ====
43 43  
44 -
45 -----
46 -
47 47  ==== ====
48 48  
49 -
50 -----
51 -
52 -
53 -----
140 +==== ====