| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Linq.Expressions; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using System.Runtime.ExceptionServices; |
| | | 10 | | using System.Text; |
| | | 11 | | using CoreWCF.Description; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Dispatcher |
| | | 15 | | { |
| | | 16 | | internal delegate object InvokeDelegate(object target, object[] inputs, object[] outputs); |
| | | 17 | | |
| | | 18 | | internal delegate IAsyncResult InvokeBeginDelegate(object target, object[] inputs, AsyncCallback asyncCallback, obje |
| | | 19 | | |
| | | 20 | | internal delegate object InvokeEndDelegate(object target, object[] outputs, IAsyncResult result); |
| | | 21 | | |
| | | 22 | | internal delegate object CreateInstanceDelegate(); |
| | | 23 | | |
| | | 24 | | internal static class InvokerUtil |
| | | 25 | | { |
| | 6 | 26 | | private static readonly string s_useLegacyInvokeDelegateAppContextSwitchKey = "CoreWCF.Dispatcher.UseLegacyInvok |
| | 6 | 27 | | private static readonly string s_isDynamicCodeSupportedAppContextSwitchKey = "System.Runtime.CompilerServices.Ru |
| | | 28 | | |
| | 6 | 29 | | private static readonly Lazy<bool> s_isDynamicCodeSupported = new Lazy<bool>(() => |
| | 6 | 30 | | // See https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Runtim |
| | 6 | 31 | | AppContext.TryGetSwitch(s_isDynamicCodeSupportedAppContextSwitchKey, out bool isDynamicCodeSupported) |
| | 6 | 32 | | ? isDynamicCodeSupported |
| | 6 | 33 | | : true |
| | 6 | 34 | | ); |
| | | 35 | | |
| | 6 | 36 | | private static readonly Lazy<bool> s_useLegacyInvokeDelegate = new Lazy<bool>(() => |
| | 6 | 37 | | AppContext.TryGetSwitch(s_useLegacyInvokeDelegateAppContextSwitchKey, out bool useLegacyInvokeDelegate) |
| | 6 | 38 | | ? useLegacyInvokeDelegate |
| | 6 | 39 | | : false |
| | 6 | 40 | | ); |
| | | 41 | | |
| | | 42 | | private const BindingFlags DefaultBindingFlags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.P |
| | | 43 | | // private readonly CriticalHelper _helper; |
| | | 44 | | |
| | | 45 | | //public InvokerUtil() |
| | | 46 | | //{ |
| | | 47 | | // _helper = new CriticalHelper(); |
| | | 48 | | //} |
| | | 49 | | |
| | | 50 | | internal static bool HasDefaultConstructor(Type type) |
| | | 51 | | { |
| | 447 | 52 | | return type.GetConstructor(DefaultBindingFlags, null, Type.EmptyTypes, null) != null; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | internal static CreateInstanceDelegate GenerateCreateInstanceDelegate(Type type) |
| | | 56 | | { |
| | 343 | 57 | | return CriticalHelper.GenerateCreateInstanceDelegate(type); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | internal static InvokeDelegate GenerateInvokeDelegate(ILogger<InvokeDelegate> logger, MethodInfo method, out int |
| | | 61 | | out int outputParameterCount) |
| | | 62 | | { |
| | 49 | 63 | | return CriticalHelper.GenerateInvokeDelegate(logger, method, out inputParameterCount, out outputParameterCou |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | //internal InvokeBeginDelegate GenerateInvokeBeginDelegate(MethodInfo method, out int inputParameterCount) |
| | | 67 | | //{ |
| | | 68 | | // return helper.GenerateInvokeBeginDelegate(method, out inputParameterCount); |
| | | 69 | | //} |
| | | 70 | | |
| | | 71 | | //internal InvokeEndDelegate GenerateInvokeEndDelegate(MethodInfo method, out int outputParameterCount) |
| | | 72 | | //{ |
| | | 73 | | // return helper.GenerateInvokeEndDelegate(method, out outputParameterCount); |
| | | 74 | | //} |
| | | 75 | | |
| | | 76 | | private static class CriticalHelper |
| | | 77 | | { |
| | | 78 | | internal static CreateInstanceDelegate GenerateCreateInstanceDelegate(Type type) |
| | | 79 | | { |
| | 343 | 80 | | if (type.GetTypeInfo().IsValueType) |
| | | 81 | | { |
| | 0 | 82 | | MethodInfo method = typeof(CriticalHelper).GetMethod(nameof(CreateInstanceOfStruct), |
| | 0 | 83 | | BindingFlags.NonPublic | BindingFlags.Static); |
| | 0 | 84 | | MethodInfo generic = method.MakeGenericMethod(type); |
| | 0 | 85 | | return (CreateInstanceDelegate)generic.CreateDelegate(typeof(CreateInstanceDelegate)); |
| | | 86 | | } |
| | | 87 | | else |
| | | 88 | | { |
| | 343 | 89 | | MethodInfo method = typeof(CriticalHelper).GetMethod(nameof(CreateInstanceOfClass), |
| | 343 | 90 | | BindingFlags.NonPublic | BindingFlags.Static); |
| | 343 | 91 | | MethodInfo generic = method.MakeGenericMethod(type); |
| | 343 | 92 | | return (CreateInstanceDelegate)generic.CreateDelegate(typeof(CreateInstanceDelegate)); |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | internal static object CreateInstanceOfClass<T>() where T : class, new() |
| | | 97 | | { |
| | 556 | 98 | | return new T(); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | internal static object CreateInstanceOfStruct<T>() where T : struct |
| | | 102 | | { |
| | 0 | 103 | | return default(T); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | internal static InvokeDelegate GenerateInvokeDelegate(ILogger<InvokeDelegate> logger, MethodInfo method, out |
| | 49 | 107 | | (!s_useLegacyInvokeDelegate.Value && s_isDynamicCodeSupported.Value) |
| | 49 | 108 | | ? GenerateInvokeDelegateInternalWithExpressions(logger, method, out inputParameterCount, out outputParam |
| | 49 | 109 | | : GenerateInvokeDelegateInternal(method, out inputParameterCount, out outputParameterCount); |
| | | 110 | | |
| | | 111 | | private static InvokeDelegate GenerateInvokeDelegateInternal(MethodInfo method, out int inputParameterCount, |
| | | 112 | | { |
| | 0 | 113 | | ParameterInfo[] parameters = method.GetParameters(); |
| | 0 | 114 | | bool returnsValue = method.ReturnType != typeof(void); |
| | 0 | 115 | | int paramCount = parameters.Length; |
| | | 116 | | |
| | 0 | 117 | | var inputParamPositions = new List<int>(); |
| | 0 | 118 | | var outputParamPositions = new List<int>(); |
| | | 119 | | |
| | 0 | 120 | | for (int i = 0; i < parameters.Length; i++) |
| | | 121 | | { |
| | 0 | 122 | | if (ServiceReflector.FlowsIn(parameters[i])) |
| | | 123 | | { |
| | 0 | 124 | | inputParamPositions.Add(i); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | if (ServiceReflector.FlowsOut(parameters[i])) |
| | | 128 | | { |
| | 0 | 129 | | outputParamPositions.Add(i); |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | int[] inputPos = inputParamPositions.ToArray(); |
| | 0 | 134 | | int[] outputPos = outputParamPositions.ToArray(); |
| | | 135 | | |
| | 0 | 136 | | inputParameterCount = inputPos.Length; |
| | 0 | 137 | | outputParameterCount = outputPos.Length; |
| | | 138 | | |
| | 0 | 139 | | return delegate (object target, object[] inputs, object[] outputs) |
| | 0 | 140 | | { |
| | 0 | 141 | | object[] paramsLocal = null; |
| | 0 | 142 | | if (paramCount > 0) |
| | 0 | 143 | | { |
| | 0 | 144 | | paramsLocal = new object[paramCount]; |
| | 0 | 145 | | |
| | 0 | 146 | | for (int i = 0; i < inputPos.Length; i++) |
| | 0 | 147 | | { |
| | 0 | 148 | | paramsLocal[inputPos[i]] = inputs[i]; |
| | 0 | 149 | | } |
| | 0 | 150 | | } |
| | 0 | 151 | | |
| | 0 | 152 | | object result = null; |
| | 0 | 153 | | try |
| | 0 | 154 | | { |
| | 0 | 155 | | if (returnsValue) |
| | 0 | 156 | | { |
| | 0 | 157 | | result = method.Invoke(target, paramsLocal); |
| | 0 | 158 | | } |
| | 0 | 159 | | else |
| | 0 | 160 | | { |
| | 0 | 161 | | method.Invoke(target, paramsLocal); |
| | 0 | 162 | | } |
| | 0 | 163 | | } |
| | 0 | 164 | | catch (TargetInvocationException tie) |
| | 0 | 165 | | { |
| | 0 | 166 | | ExceptionDispatchInfo.Capture(tie.InnerException).Throw(); |
| | 0 | 167 | | } |
| | 0 | 168 | | |
| | 0 | 169 | | for (int i = 0; i < outputPos.Length; i++) |
| | 0 | 170 | | { |
| | 0 | 171 | | Debug.Assert(paramsLocal != null); |
| | 0 | 172 | | outputs[i] = paramsLocal[outputPos[i]]; |
| | 0 | 173 | | } |
| | 0 | 174 | | |
| | 0 | 175 | | return result; |
| | 0 | 176 | | }; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | private static InvokeDelegate GenerateInvokeDelegateInternalWithExpressions(ILogger<InvokeDelegate> logger, |
| | | 180 | | { |
| | 49 | 181 | | inputParameterCount = 0; |
| | 49 | 182 | | outputParameterCount = 0; |
| | 49 | 183 | | ParameterInfo[] parameters = method.GetParameters(); |
| | 49 | 184 | | bool returnsValue = method.ReturnType != typeof(void); |
| | 49 | 185 | | bool returnsValueType = method.ReturnType.IsValueType; |
| | | 186 | | |
| | 49 | 187 | | var targetParam = Expression.Parameter(typeof(object), "target"); |
| | 49 | 188 | | var inputsParam = Expression.Parameter(typeof(object[]), "inputs"); |
| | 49 | 189 | | var outputsParam = Expression.Parameter(typeof(object[]), "outputs"); |
| | | 190 | | |
| | 49 | 191 | | List<ParameterExpression> variables = new(); |
| | 49 | 192 | | var result = Expression.Variable(typeof(object), "result"); |
| | 49 | 193 | | variables.Add(result); |
| | | 194 | | |
| | 49 | 195 | | List<(Type ParameterType, ParameterExpression OutputExpression)> outputVariables = new(); |
| | 49 | 196 | | List<ParameterExpression> invocationParameters = new(); |
| | 49 | 197 | | List<Expression> expressions = new(); |
| | | 198 | | |
| | 136 | 199 | | for (int i = 0; i < parameters.Length; i++) |
| | | 200 | | { |
| | 19 | 201 | | Type variableType = parameters[i].ParameterType.IsByRef |
| | 19 | 202 | | ? parameters[i].ParameterType.GetElementType() |
| | 19 | 203 | | : parameters[i].ParameterType; |
| | 19 | 204 | | ParameterExpression variable = Expression.Variable(variableType, $"p{i}"); |
| | | 205 | | |
| | 19 | 206 | | if (ServiceReflector.FlowsIn(parameters[i])) |
| | | 207 | | { |
| | 19 | 208 | | var inputParamValue = |
| | 19 | 209 | | Expression.ArrayIndex(inputsParam, Expression.Constant(inputParameterCount)); |
| | 19 | 210 | | var convertValueToType = Expression.Convert(inputParamValue, variableType); |
| | 19 | 211 | | var defaultValue = Expression.Default(variableType); |
| | 19 | 212 | | var convertOrGetDefault = Expression.Condition(Expression.Equal(inputParamValue, Expression.Cons |
| | 19 | 213 | | defaultValue, convertValueToType); |
| | 19 | 214 | | expressions.Add(Expression.Assign(variable, convertOrGetDefault)); |
| | 19 | 215 | | inputParameterCount++; |
| | | 216 | | } |
| | | 217 | | |
| | 19 | 218 | | if (ServiceReflector.FlowsOut(parameters[i])) |
| | | 219 | | { |
| | 0 | 220 | | outputParameterCount++; |
| | 0 | 221 | | outputVariables.Add((variableType, variable)); |
| | | 222 | | } |
| | | 223 | | |
| | 19 | 224 | | variables.Add(variable); |
| | 19 | 225 | | invocationParameters.Add(variable); |
| | | 226 | | } |
| | | 227 | | |
| | 49 | 228 | | var castTargetParam = Expression.Convert(targetParam, method.DeclaringType); |
| | | 229 | | |
| | 49 | 230 | | if (returnsValue) |
| | | 231 | | { |
| | 35 | 232 | | if (returnsValueType) |
| | | 233 | | { |
| | 0 | 234 | | expressions.Add(Expression.Assign(result, Expression.Convert(Expression.Call(castTargetParam, me |
| | | 235 | | } |
| | | 236 | | else |
| | | 237 | | { |
| | 35 | 238 | | expressions.Add(Expression.Assign(result, Expression.Call(castTargetParam, method, invocationPar |
| | | 239 | | } |
| | | 240 | | } |
| | | 241 | | else |
| | | 242 | | { |
| | 14 | 243 | | expressions.Add(Expression.Call(castTargetParam, method, invocationParameters)); |
| | 14 | 244 | | expressions.Add(Expression.Assign(result, Expression.Constant(null, typeof(object)))); |
| | | 245 | | } |
| | | 246 | | |
| | 49 | 247 | | int j = 0; |
| | 98 | 248 | | foreach (var outputVariable in outputVariables) |
| | | 249 | | { |
| | 0 | 250 | | if (outputVariable.ParameterType.IsValueType) |
| | | 251 | | { |
| | 0 | 252 | | expressions.Add(Expression.Assign( |
| | 0 | 253 | | Expression.ArrayAccess(outputsParam, Expression.Constant(j)), |
| | 0 | 254 | | Expression.Convert(outputVariable.OutputExpression, typeof(object)))); |
| | | 255 | | } |
| | | 256 | | else |
| | | 257 | | { |
| | 0 | 258 | | expressions.Add(Expression.Assign( |
| | 0 | 259 | | Expression.ArrayAccess(outputsParam, Expression.Constant(j)), |
| | 0 | 260 | | outputVariable.OutputExpression)); |
| | | 261 | | } |
| | 0 | 262 | | j++; |
| | | 263 | | } |
| | | 264 | | |
| | 49 | 265 | | expressions.Add(result); |
| | | 266 | | |
| | 49 | 267 | | BlockExpression finalBlock = Expression.Block(variables: variables, expressions: expressions); |
| | | 268 | | |
| | 49 | 269 | | Expression<InvokeDelegate> lambda = Expression.Lambda<InvokeDelegate>( |
| | 49 | 270 | | finalBlock, |
| | 49 | 271 | | targetParam, |
| | 49 | 272 | | inputsParam, |
| | 49 | 273 | | outputsParam); |
| | | 274 | | |
| | 49 | 275 | | if (logger.IsEnabled(LogLevel.Debug)) |
| | | 276 | | { |
| | 0 | 277 | | var expr = GetDebugString(finalBlock).Trim(); |
| | 0 | 278 | | logger.LogDebug("{methodType} {method}\n{expr}", method.DeclaringType, method.ToString(), expr); |
| | | 279 | | } |
| | | 280 | | |
| | 49 | 281 | | return lambda.Compile(); |
| | | 282 | | |
| | | 283 | | string GetDebugString(Expression expr) |
| | | 284 | | { |
| | 0 | 285 | | if (expr is BlockExpression block) |
| | | 286 | | { |
| | 0 | 287 | | StringBuilder sb = new(); |
| | 0 | 288 | | foreach (var e in block.Expressions) |
| | | 289 | | { |
| | 0 | 290 | | sb.AppendLine(GetDebugString(e)); |
| | | 291 | | } |
| | 0 | 292 | | return sb.ToString(); |
| | | 293 | | } |
| | | 294 | | else |
| | | 295 | | { |
| | 0 | 296 | | return expr.ToString(); |
| | | 297 | | } |
| | | 298 | | } |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | //public InvokeBeginDelegate GenerateInvokeBeginDelegate(MethodInfo method, out int inputParameterCount) |
| | | 302 | | //{ |
| | | 303 | | // ParameterInfo[] parameters = method.GetParameters(); |
| | | 304 | | // var inputCount = parameters.Length; |
| | | 305 | | // inputParameterCount = inputCount; |
| | | 306 | | |
| | | 307 | | // InvokeBeginDelegate lambda = |
| | | 308 | | // delegate(object target, object[] inputs, AsyncCallback callback, object state) |
| | | 309 | | // { |
| | | 310 | | // object[] inputsLocal = new object[inputCount]; |
| | | 311 | | // for (var i = 0; i < inputCount; i++) |
| | | 312 | | // { |
| | | 313 | | // inputsLocal[i] = inputs[i]; |
| | | 314 | | // } |
| | | 315 | | // inputsLocal[inputCount] = callback; |
| | | 316 | | // inputsLocal[inputCount + 1] = state; |
| | | 317 | | // object result = method.Invoke(target, inputs); |
| | | 318 | | // return result as IAsyncResult; |
| | | 319 | | // }; |
| | | 320 | | |
| | | 321 | | // return lambda; |
| | | 322 | | //} |
| | | 323 | | |
| | | 324 | | //public InvokeEndDelegate GenerateInvokeEndDelegate(MethodInfo method, out int outParameterCount) |
| | | 325 | | //{ |
| | | 326 | | |
| | | 327 | | // InvokeEndDelegate lambda = |
| | | 328 | | // delegate(object target, object[] outputs, IAsyncResult result) |
| | | 329 | | // { |
| | | 330 | | |
| | | 331 | | // } |
| | | 332 | | //} |
| | | 333 | | } |
| | | 334 | | } |
| | | 335 | | } |