private NewExpression BuildNewExpression() { ConstructorInfo[] constructors = null; if (typeof(IAopSupport).IsAssignableFrom(typeof(TImplementation))) { constructors = AspectFactory.GetProxyType(typeof(TImplementation)).GetConstructors(); } else { constructors = typeof(TImplementation).GetConstructors(); } if (constructors.Length != 1) { throw new InvalidOperationException(SR.GetString(SRKind.NoDefaultConstructor)); } var constructor = constructors[0]; foreach (var par in constructor.GetParameters()) { if (!_container.IsRegistered(par.ParameterType)) { throw new InvalidOperationException(SR.GetString(SRKind.NotFoundRegisterForType, par.ParameterType)); } } var arguments = from parameter in constructor.GetParameters() select BuildParameterExpression(parameter.ParameterType); return(Expression.New(constructor, arguments.ToArray())); }
/// <summary> /// 检查是否支持 <see cref="IAopSupport"/> 接口。 /// </summary> /// <param name="type"></param> /// <returns></returns> private static Type CheckAopProxyType(Type type) { if (typeof(IAopSupport).IsAssignableFrom(type) && !typeof(IAopImplement).IsAssignableFrom(type)) { return(AspectFactory.GetProxyType(type)); } return(type); }
private NewExpression BuildNewExpression() { //检查是否可构造 Helpers.CheckConstructable(typeof(TImplementation)); //获取默认的构造函数 ConstructorInfo constructor = null; if (typeof(IAopSupport).IsAssignableFrom(typeof(TImplementation))) { constructor = AspectFactory.GetProxyType(typeof(TImplementation)).GetConstructors().Single(); } else { constructor = typeof(TImplementation).GetConstructors().Single(); } var arguments = from parameter in constructor.GetParameters() select BuildParameterExpression(parameter.ParameterType); return(Expression.New(constructor, arguments.ToArray())); }