public static DefaultConstructorHandler CreateDefaultConstructorMethod(Type type)
        {
            if (type == Types.String)
            {
                DefaultConstructorHandler s = () => null;
                return(s);
            }

            //if (binderType.IsValueType)
            //{
            //    var dm = new DynamicMethod(string.Empty, binderType, Type.EmptyTypes, Module, true);
            //    var ilGen = dm.GetILGenerator();
            //    ilGen.Emit(OpCodes.Ldloc_0);
            //    ilGen.Emit(OpCodes.Stloc_1);
            //    ilGen.Emit(OpCodes.Br_S);
            //    ilGen.Emit(OpCodes.Ldloc_1);
            //    ilGen.Emit(OpCodes.Ret);
            //    return dm.CreateDelegate(typeof(DefaultConstructorHandler)) as DefaultConstructorHandler;
            //}

            var ctorExpression = Expression
                                 .Lambda <DefaultConstructorHandler>(
                Expression
                .Convert(Expression.New(type), typeof(object)));

            return(ctorExpression.Compile());
        }
示例#2
0
 public ClassMapper(Type fromType, Type toType)
     : base(fromType, toType)
 {
     if (!toType.IsAbstract)
     {
         if (toType.IsNullable())
             Ctor = DynamicMethodFactory.GetDefaultCreator(Nullable.GetUnderlyingType(toType));
         else
             Ctor = DynamicMethodFactory.GetDefaultCreator(toType);
     }
 }
示例#3
0
 public DataRowMapper(Type toType)
     : base(typeof(DataRow), toType)
 {
     if (!toType.IsAbstract)
     {
         if (toType.IsNullable())
             Ctor = DynamicMethodFactory.GetDefaultCreator(Nullable.GetUnderlyingType(toType));
         else
             Ctor = DynamicMethodFactory.GetDefaultCreator(toType);
     }
 }
示例#4
0
 public DataRowMapper(Type toType)
     : base(typeof(DataRow), toType)
 {
     if (!toType.IsAbstract)
     {
         if (toType.IsNullable())
             Ctor = DynamicMethodFactory.GetDefaultCreator(Nullable.GetUnderlyingType(toType));
         else
             Ctor = DynamicMethodFactory.GetDefaultCreator(toType);
     }
 }
示例#5
0
 public ClassMapper(Type fromType, Type toType)
     : base(fromType, toType)
 {
     if (!toType.IsAbstract)
     {
         if (toType.IsNullable())
         {
             Ctor = DynamicMethodFactory.GetDefaultCreator(Nullable.GetUnderlyingType(toType));
         }
         else
         {
             Ctor = DynamicMethodFactory.GetDefaultCreator(toType);
         }
     }
 }
示例#6
0
            private static object ToEntitySet <T>(IEnumerable <T> items)
            {
                var entitySetType = EntitySetType.MakeGenericType(typeof(T));
                var key           = entitySetType.TypeHandle.Value.GetHashCode();
                DefaultConstructorHandler ctor = null;

                if (!EntitySetCtorCache.TryGetValue(key, out ctor))
                {
                    ctor = entitySetType.GetDefaultCreator();
                    lock (EntitySetCtorCache)
                        EntitySetCtorCache[key] = ctor;
                }
                var entitySet = ctor() as IList <T>;

                foreach (var item in items)
                {
                    entitySet.Add(item);
                }
                return(entitySet);
            }
示例#7
0
        /// <summary>
        /// 得到缺省构造函数委托
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DefaultConstructorHandler GetDefaultCreator(this Type type)
        {
            Guard.NotNull(type, "type");
            var ctor = DefaultDynamicMethodFactory.CreateDefaultConstructorMethod(type);

            DefaultConstructorHandler handler = () =>
            {
                try
                {
                    return(ctor());
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            };

            return(handler);
        }