Esempio n. 1
0
        public List <IMapable> ExecuteReader(DelegateMap customMap)
        {
            try
            {
                // Execute the SP and get the SqlDataReader
                SqlDataReader reader = ExecuteReader();

                // Map the Entities
                List <IMapable> map = new List <IMapable>();

                while (reader.Read())
                {
                    map.Add(customMap(reader));
                }

                // Close the SqlDataReader
                if (!reader.IsClosed)
                {
                    reader.Close();
                }

                return(map);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        private static Delegate BuildDelegate(DelegateMap map, Action <ILGenerator> emitInvoke)
        {
            var pm         = map.ParametersMap;
            var paramTypes = pm.ConvertAll(x => x.Delegate.Type);
            var m          = new DynamicMethod(string.Empty, map.RetValMap.Delegate, paramTypes,
                                               typeof(EmitDelegateBuilder), MemberInfoEx.PrivateAccess);
            var il = m.GetILGenerator();
            // Prepare parameters...
            var locals = pm.ConvertAll((p, i) => EmitPrepare(il, p, i));

            // Load parameters, stack should be empty here
            for (var i = 0; i < pm.Length; i++)
            {
                EmitLoad(il, pm[i], i, locals[i]);
            }
            // Emit invoke
            emitInvoke(il);
            // Emit finish, stack should contain return value here (if not void)
            for (var i = 0; i < pm.Length; i++)
            {
                EmitFinish(il, pm[i], i, locals[i]);
            }
            // Emit return
            EmitConvertReturn(il, map.RetValMap);
            il.Emit(OpCodes.Ret);
            return(m.CreateDelegate(map.DelegateType));
        }
Esempio n. 3
0
 public Delegate FieldSet(FieldInfo field, Type delegateType)
 {
     return(BuildDelegate(DelegateMap.FieldSet(delegateType, field),
                          parameters =>
     {
         var instance = parameters.Length > 1 ? parameters[0] : null;
         var value = parameters.Length > 1 ? parameters[1] : parameters[0];
         var fieldExpr = Expression.Field(instance, field);
         return Expression.Assign(fieldExpr, value);
     }));
 }
 public Delegate FieldGet(FieldInfo field, Type delegateType)
 {
     return(BuildDelegate(DelegateMap.FieldGet(delegateType, field),
                          il =>
     {
         if (field.IsLiteral)
         {
             var value = field.GetValue(null);
             il.EmitLdConst(value);
         }
         else
         {
             il.EmitFieldGet(field);
         }
     }));
 }
        public Delegate Method(MethodInfo method, Type delegateType)
        {
            var map       = DelegateMap.Method(delegateType, method);
            var declaring = method.DeclaringType;

            // avoid stupid issue in .Net framework... with no CreateDelegate for Generic Interface methods
            if (declaring == null || !(declaring.IsInterface && method.IsGenericMethod))
            {
                // if exact match
                if (map.RetValMap.Matches && Array.TrueForAll(map.ParametersMap, x => x.Matches))
                {
                    return(Delegate.CreateDelegate(delegateType, method));
                }
            }
            // TODO: Simple Delegate.CreateDelegate
            return(BuildDelegate(map,
                                 il => il.EmitMethodCall(method)));
        }
Esempio n. 6
0
        private static Delegate BuildDelegate(DelegateMap map, Func <Expression[], Expression> buildInvoke)
        {
            var pm               = map.ParametersMap;
            var parameters       = pm.ConvertAll(p => Expression.Parameter(p.Delegate.Type));
            var block            = new ExpressionBlockBuilder();
            var invokeParameters = parameters.ConvertAll((p, i) => Prepare(pm[i], block, p));
            var invoke           = buildInvoke(invokeParameters);
            var retVal           = PrepareInvoke(map.RetValMap, block, invoke);

            for (var i = 0; i < parameters.Length; i++)
            {
                Finish(pm[i], block, invokeParameters[i], parameters[i]);
            }
            ConvertReturn(map.RetValMap, block, invoke, retVal);

            var body   = block.CreateExpression();
            var lambda = Expression.Lambda(map.DelegateType, body, parameters);

            return(lambda.Compile());
        }
Esempio n. 7
0
        public Delegate Method(MethodInfo method, Type delegateType)
        {
            return(BuildDelegate(DelegateMap.Method(delegateType, method), parameters =>
            {
                Expression instance;
                IEnumerable <Expression> invokeParams;
                if (method.IsStatic)
                {
                    instance = null;
                    invokeParams = parameters;
                }
                else
                {
                    instance = parameters[0];
                    invokeParams = parameters.Skip(1);
                }

                return Expression.Call(instance, method, invokeParams);
            }));
        }
Esempio n. 8
0
 public Delegate FieldGet(FieldInfo field, Type delegateType)
 {
     return(BuildDelegate(DelegateMap.FieldGet(delegateType, field),
                          parameters => Expression.Field(parameters.Length != 0 ? parameters[0] : null, field)));
 }
Esempio n. 9
0
 public Delegate Constructor(ConstructorInfo constructor, Type delegateType)
 {
     return(BuildDelegate(DelegateMap.Constructor(delegateType, constructor),
                          parameters => Expression.New(constructor, parameters)));
 }
Esempio n. 10
0
 public IEnumerable <IMappingStrategy> GetStrategies()
 {
     yield return(DelegateMap.Map((Car c) => TinyMapper.Map <CarSummary>(c)));
 }
Esempio n. 11
0
 public Delegate FieldSet(FieldInfo field, Type delegateType)
 {
     return(BuildDelegate(DelegateMap.FieldSet(delegateType, field),
                          il => il.EmitFieldSet(field)));
 }
Esempio n. 12
0
 public Delegate Constructor(ConstructorInfo constructor, Type delegateType)
 {
     return(BuildDelegate(DelegateMap.Constructor(delegateType, constructor),
                          il => il.Emit(OpCodes.Newobj, constructor)));
 }