| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Concurrent; |
| | 6 | | using System.Linq.Expressions; |
| | 7 | | using System.Reflection; |
| | 8 | | using Azure.Data.Tables.Queryable; |
| | 9 | | using EntityActivator = System.Func<object[], object>; |
| | 10 | |
|
| | 11 | | namespace Azure.Data.Tables |
| | 12 | | { |
| | 13 | | internal static class EntityUtilities |
| | 14 | | { |
| | 15 | | internal static object InstantiateEntityFromType(Type type) |
| | 16 | | { |
| 0 | 17 | | EntityActivator activator = compiledActivators.GetOrAdd(type, GenerateActivator); |
| 0 | 18 | | return activator(null /* no params */); |
| | 19 | | } |
| | 20 | |
|
| | 21 | | private static EntityActivator GenerateActivator(Type type) |
| | 22 | | { |
| | 23 | | // Generate activator for parameterless constructor |
| 0 | 24 | | return GenerateActivator(type, System.Type.EmptyTypes); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | private static EntityActivator GenerateActivator(Type type, Type[] ctorParamTypes) |
| | 28 | | { |
| 0 | 29 | | ConstructorInfo constructorInfo = type.GetConstructor(ctorParamTypes); |
| 0 | 30 | | if (constructorInfo == null) |
| | 31 | | { |
| 0 | 32 | | throw new InvalidOperationException(SR.TableQueryTypeMustHaveDefaultParameterlessCtor); |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | ParameterInfo[] parameterInfos = constructorInfo.GetParameters(); |
| | 36 | |
|
| | 37 | | // Create a single param of type object[] |
| 0 | 38 | | ParameterExpression parameterExpression = Expression.Parameter(typeof(object[]), "args"); |
| 0 | 39 | | Expression[] argsExp = new Expression[parameterInfos.Length]; |
| | 40 | |
|
| | 41 | | // Pick each arg from the params array and create a typed expression of them |
| 0 | 42 | | for (int i = 0; i < parameterInfos.Length; i++) |
| | 43 | | { |
| 0 | 44 | | Expression index = Expression.Constant(i); |
| 0 | 45 | | Type paramType = parameterInfos[i].ParameterType; |
| 0 | 46 | | Expression paramAccessorExp = Expression.ArrayIndex(parameterExpression, index); |
| 0 | 47 | | Expression paramCastExp = Expression.Convert(paramAccessorExp, paramType); |
| 0 | 48 | | argsExp[i] = paramCastExp; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | // Make a NewExpression that calls the ctor with the args we just created |
| 0 | 52 | | NewExpression newExpression = Expression.New(constructorInfo, argsExp); |
| | 53 | |
|
| | 54 | | // Create a lambda with the New Expression as body and our param object[] as arg |
| 0 | 55 | | LambdaExpression lambda = Expression.Lambda(typeof(EntityActivator), newExpression, parameterExpression); |
| | 56 | |
|
| | 57 | | // Compile it |
| 0 | 58 | | return (EntityActivator)lambda.Compile(); |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | private static ConcurrentDictionary<Type, EntityActivator> compiledActivators = new ConcurrentDictionary<Type, E |
| | 62 | | } |
| | 63 | | } |