Пример #1
0
        public override void PrecompileCollection <T, TN>()
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(typeof(T), typeof(TN));

            if (CollectionMappers.ContainsKey(cacheKey))
            {
                return;
            }

            var sourceType = GetCollectionElementType(typeof(T));
            var destType   = GetCollectionElementType(typeof(TN));

            var sourceVariable = Expression.Parameter(typeof(T), "source");
            var destVariable   = Expression.Parameter(typeof(TN), "dst");

            var srcCount  = Expression.Call(typeof(Enumerable), "Count", new[] { sourceType }, sourceVariable);
            var destCount = Expression.Call(typeof(Enumerable), "Count", new[] { destType }, destVariable);

            var conditionToCreateList = Expression.NotEqual(srcCount, destCount);
            var notNullCondition      = Expression.IfThenElse(conditionToCreateList,
                                                              MapCollectionNotCountEquals(typeof(T), typeof(TN), sourceVariable, destVariable),
                                                              MapCollectionCountEquals(typeof(T), typeof(TN), sourceVariable, destVariable));

            var newCollBlockExp = CompileCollectionInternal <T, TN>(sourceVariable, destVariable);

            var result = Expression.IfThenElse(Expression.NotEqual(destVariable, StaticExpressions.NullConstant),
                                               notNullCondition,
                                               newCollBlockExp);

            var expressions = new List <Expression> {
                result
            };

            var resultExpression = Expression.Block(new ParameterExpression[] {}, expressions);

            var checkSrcForNullExp =
                Expression.IfThenElse(Expression.Equal(sourceVariable, StaticExpressions.NullConstant),
                                      Expression.Default(destVariable.Type), resultExpression);
            var block        = Expression.Block(new ParameterExpression[] {}, checkSrcForNullExp, destVariable);
            var lambda       = Expression.Lambda <Func <T, TN, TN> >(block, sourceVariable, destVariable);
            var compiledFunc = lambda.Compile();

            CollectionMappers[cacheKey] = compiledFunc;
        }
Пример #2
0
 public override MulticastDelegate MapCollection(long cacheKey)
 {
     return(CollectionMappers.ContainsKey(cacheKey) ? CollectionMappers[cacheKey] : null);
 }