public void Resolver(IResolverContext context, IDependencyResolver resolver)
 {
     context.Resolved = Expression.Lambda <Func <IDependencyResolver, object[], object> >(
         Expression.New(context.IsDynamicProxy()
             ? DynamicTypeCacheManager.GetCache(context.Dependency.ImplementationType)
             : context.Dependency.ImplementationType),
         Expression.Parameter(typeof(IDependencyResolver)),
         Expression.Parameter(typeof(object[])));
 }
        public void Resolver(IResolverContext context, IDependencyResolver resolver)
        {
            var implType = context.IsDynamicProxy()
                ? DynamicTypeCacheManager.GetCache(context.Dependency.ImplementationType)
                : context.Dependency.ImplementationType;

            var constructor = ResolverHelper.GetBastConstructor(implType, resolver);

            if (constructor == null)
            {
                throw new InvalidOperationException($"{implType.FullName}不存在公共构造方法。");
            }

            var parameter = Expression.Parameter(typeof(object[]), "args");

            context.Resolved = Expression.
                               Lambda <Func <IDependencyResolver, object[], object> >(
                Expression.New(constructor,
                               GetConstructorParameters(constructor, parameter)),
                Expression.Parameter(typeof(IDependencyResolver)),
                parameter);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     使用与指定参数匹配程度最高的构造函数创建指定类型的代理实例。
        /// </summary>
        public static object CreateInstance(Type type, params object[] args)
        {
            var proxyType = DynamicTypeCacheManager.GetCache(type);

            return(InstanceCacheManger.Cache(proxyType, args));
        }