Exemple #1
0
        private Func <DbDataReader, object> GetCreateDelegate()
        {
            if (_mapperCollection.HasFactory(_type))
            {
                return(dataReader => _mapperCollection.GetFactory(_type)(dataReader));
            }

            var constructorInfo = _type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).SingleOrDefault(x => x.GetParameters().Length == 0);

            if (constructorInfo == null)
            {
                return(_ => null);
            }

            // var poco=new T()
            var constructor = new DynamicMethod(Guid.NewGuid().ToString(), _type, new[] { typeof(DbDataReader) }, true);
            var il          = constructor.GetILGenerator();

            il.Emit(OpCodes.Newobj, constructorInfo);
            il.Emit(OpCodes.Ret);

            try
            {
                var del = constructor.CreateDelegate(Expression.GetFuncType(typeof(DbDataReader), typeof(object)));
                return(del as Func <DbDataReader, object>);
            }
            catch (Exception exception)
            {
                throw new Exception("Error trying to create type " + _type, exception);
            }
        }
Exemple #2
0
        private Func <DbDataReader, object> GetCreateDelegate()
        {
            if (_mapperCollection.HasFactory(_type))
            {
                return(dataReader => _mapperCollection.GetFactory(_type)(dataReader));
            }

            if (_constructorInfo == null)
            {
                return(_ => null);
            }

            var create     = CreateObjectFactoryMethodWithCtorParams(_constructorInfo);
            var parameters = _constructorInfo.GetParameters()
                             .Select(x => MappingHelper.GetDefault(x.ParameterType))
                             .ToArray();

            return(x => create(parameters));
        }