| | | 1 | | // |
| | | 2 | | // Copyright 2011 Ekon Benefits |
| | | 3 | | // |
| | | 4 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | | 5 | | // you may not use this file except in compliance with the License. |
| | | 6 | | // You may obtain a copy of the License at |
| | | 7 | | // |
| | | 8 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | | 9 | | // |
| | | 10 | | // Unless required by applicable law or agreed to in writing, software |
| | | 11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | | 12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | 13 | | // See the License for the specific language governing permissions and |
| | | 14 | | // limitations under the License. |
| | | 15 | | |
| | | 16 | | using System; |
| | | 17 | | using System.Collections.Concurrent; |
| | | 18 | | using System.Collections.Generic; |
| | | 19 | | using System.Linq; |
| | | 20 | | using System.Linq.Expressions; |
| | | 21 | | using Microsoft.CSharp.RuntimeBinder; |
| | | 22 | | using Binder = Microsoft.CSharp.RuntimeBinder.Binder; |
| | | 23 | | |
| | | 24 | | namespace ImpromptuInterface.Optimization |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// `new T(args)` with the constructor chosen by the arguments' runtime types, as C#'s |
| | | 28 | | /// `new T(dynamicArg)` does — rather than by <see cref="Activator"/>, whose overload |
| | | 29 | | /// resolution is not the same. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <remarks> |
| | | 32 | | /// The call site is built once per (type, argument count) and cached: the shape is what a |
| | | 33 | | /// site is compiled for, and the arguments' runtime types are what it dispatches on. |
| | | 34 | | /// </remarks> |
| | | 35 | | internal static class DynamicConstructor |
| | | 36 | | { |
| | 1 | 37 | | private static readonly ConcurrentDictionary<Tuple<Type, int>, Func<object[], object>> _sites = |
| | 1 | 38 | | new ConcurrentDictionary<Tuple<Type, int>, Func<object[], object>>(); |
| | | 39 | | |
| | | 40 | | /// <summary>Constructs <paramref name="type"/> with <paramref name="args"/>.</summary> |
| | | 41 | | internal static object Invoke(Type type, params object[] args) |
| | | 42 | | { |
| | 14 | 43 | | if (type == null) throw new ArgumentNullException(nameof(type)); |
| | 14 | 44 | | args = args ?? new object[0]; |
| | | 45 | | |
| | | 46 | | // A dynamic invocation does not see a value type's parameterless constructor. |
| | 14 | 47 | | if (args.Length == 0 && type.IsValueType) |
| | 1 | 48 | | return Activator.CreateInstance(type); |
| | | 49 | | |
| | 13 | 50 | | var site = _sites.GetOrAdd(Tuple.Create(type, args.Length), key => Compile(key.Item1, key.Item2)); |
| | 13 | 51 | | return site(args); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private static Func<object[], object> Compile(Type type, int count) |
| | | 55 | | { |
| | 4 | 56 | | var argsParameter = Expression.Parameter(typeof(object[]), "args"); |
| | | 57 | | |
| | | 58 | | // The type is the site's first argument, as a static type; each real argument |
| | | 59 | | // arrives as object, so the binder dispatches on its runtime type. |
| | 4 | 60 | | var infos = new List<CSharpArgumentInfo> |
| | 4 | 61 | | { |
| | 4 | 62 | | CSharpArgumentInfo.Create( |
| | 4 | 63 | | CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null) |
| | 4 | 64 | | }; |
| | 4 | 65 | | var arguments = new List<Expression> { Expression.Constant(type, typeof(Type)) }; |
| | | 66 | | |
| | 16 | 67 | | for (var i = 0; i < count; i++) |
| | | 68 | | { |
| | 4 | 69 | | infos.Add(CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)); |
| | 4 | 70 | | arguments.Add(Expression.ArrayIndex(argsParameter, Expression.Constant(i))); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // The site's result type has to be one the binder can produce. For a value type the |
| | | 74 | | // binder produces the value type itself, and asking for object instead fails with |
| | | 75 | | // "The result type ... is not compatible with the result type ... expected by the |
| | | 76 | | // call site" - so ask for the type and box afterwards. |
| | 4 | 77 | | var resultType = type.IsValueType ? type : typeof(object); |
| | | 78 | | |
| | 4 | 79 | | Expression call = Expression.Dynamic( |
| | 4 | 80 | | Binder.InvokeConstructor(CSharpBinderFlags.None, type, infos), |
| | 4 | 81 | | resultType, |
| | 4 | 82 | | arguments); |
| | | 83 | | |
| | 4 | 84 | | if (resultType != typeof(object)) |
| | 1 | 85 | | call = Expression.Convert(call, typeof(object)); |
| | | 86 | | |
| | 4 | 87 | | return Expression.Lambda<Func<object[], object>>(call, argsParameter).Compile(); |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |