/// <summary> /// 得到构造函数委托 /// </summary> /// <param name="constructor">构造函数</param> /// <returns>返回构造函数委托</returns> public static ConstructorHandler GetCreator(this System.Reflection.ConstructorInfo constructor) { Guard.NotNull(constructor, "constructor"); ConstructorHandler ctor = constructor.DeclaringType.IsValueType ? (args) => constructor.Invoke(args) : DefaultDynamicMethodFactory.CreateConstructorMethod(constructor); ConstructorHandler handler = args => { if (args == null) { args = new object[constructor.GetParameters().Length]; } try { return(ctor(args)); } catch (TargetInvocationException ex) { throw ex.InnerException; } catch (Exception ex) { throw ex; } }; return(handler); }
private static object ToEntityRef <T>(IEnumerable <T> items) { var entityRefType = EntityRefType.MakeGenericType(typeof(T)); var key = entityRefType.TypeHandle.Value.GetHashCode(); ConstructorHandler ctor = null; if (!EntityRefCtorCache.TryGetValue(key, out ctor)) { ctor = entityRefType.GetConstructor(new Type[] { typeof(T) }).GetCreator();; lock (EntityRefCtorCache) EntityRefCtorCache[key] = ctor; } var entityRef = ctor(items.FirstOrDefault()); return(entityRef); }
public IBinding WithFactory(ConstructorHandler constructorHandler) { Factory = constructorHandler; return(this); }