Beispiel #1
0
        /// <summary>
        /// 将拦截器融合入源类生成代理类
        /// </summary>
        /// <param name="source">源类的类型</param>
        /// <param name="h">拦截器</param>
        /// <param name="createMode">代理类创建模式</param>
        /// <returns>代理类</returns>
        public dynamic NewInstanceProxy(Type source, IInterceptorHandler h, ProxyCreateMode createMode)
        {
            Type   dynamicProxyClassType = null;
            string proxyClassName        = source.Name + ConstVar.ProxyClassSuffix;

            try
            {
                lock (cache)
                {
                    if (cache.ContainsKey(proxyClassName))
                    {
                        dynamicProxyClassType = cache[proxyClassName];
                    }
                    else
                    {
                        dynamicProxyClassType = DefineClass(proxyClassName, source, createMode);
                        cache.Add(proxyClassName, dynamicProxyClassType);
                        assembly.Save(moduleName);
                    }
                }
            }
            catch (Exception ex)
            {
                var exception = new CanNotCreateException(source.Name + "代理类", ex);
                throw exception;
            }

            var constructor = dynamicProxyClassType.GetConstructor(new[] { typeof(IInterceptorHandler) });
            var proxyObj    = constructor.Invoke(new object[] { h });

            return(proxyObj);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="className"></param>
        /// <param name="parentType"></param>
        /// <param name="createMode"></param>
        /// <returns></returns>
        private Type DefineClass(string className, Type parentType, ProxyCreateMode createMode)
        {
            Type proxyType = null;
            IProxyClassBuilder proxyClassBuilder = null;

            #region 根据不同的创建类型选择创建类
            switch (createMode)
            {
            case ProxyCreateMode.WrapInterface:
                proxyClassBuilder = new WrapInterfaceClassBuilder();
                break;

            case ProxyCreateMode.WrapClass:
            default:
                proxyClassBuilder = new WrapCalssBuilder();
                break;
            }

            #endregion

            if (proxyClassBuilder.Filter == null)
            {
                proxyClassBuilder.Filter = new List <IMethodsSearchFilter>();
            }
            proxyClassBuilder.Filter.AddRange(this.Filter);

            proxyType = proxyClassBuilder.DefineClass(module, className, parentType);
            return(proxyType);
        }
Beispiel #3
0
        /// <summary>
        /// 将拦截器融合入源类生成代理类
        /// </summary>
        /// <param name="source">源类的某个实例</param>
        /// <param name="h">拦截器</param>
        /// <param name="createMode">代理类创建模式</param>
        /// <returns>代理类</returns>
        public dynamic NewInstanceProxy <T>(T source, IInterceptorHandler h, ProxyCreateMode createMode)
        {
            var obj = this.NewInstanceProxy(typeof(T), h, createMode);

            return(obj);
        }