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()); }
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); } }
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); } }
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); } } }
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); }
/// <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); }