< Summary

Class:Azure.Data.Tables.EntityUtilities
Assembly:Azure.Data.Tables
File(s):C:\Git\azure-sdk-for-net\sdk\tables\Azure.Data.Tables\src\EntityUtilities.cs
Covered lines:0
Uncovered lines:19
Coverable lines:19
Total lines:63
Line coverage:0% (0 of 19)
Covered branches:0
Total branches:4
Branch coverage:0% (0 of 4)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
InstantiateEntityFromType(...)-0%100%
GenerateActivator(...)-0%100%
GenerateActivator(...)-0%0%
.cctor()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\tables\Azure.Data.Tables\src\EntityUtilities.cs

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