Пример #1
0
        /// <summary>
        /// 生成代理类型
        /// </summary>
        /// <param name="interfaceType">实现的接口或继承</param>
        /// <param name="implementationType">实现的类型</param>
        /// <param name="Inherit">是否通过继承生成</param>
        /// <returns></returns>
        public static Type CreateProxyClassType(Type interfaceType, Type implementationType, bool Inherit = false)
        {
            // ASP.NET Core 中,可能只有接口
            if (implementationType == null)
            {
                return(null);
            }

            Type type = implementationType;

            if (implementationType.GetCustomAttribute(typeof(InterceptorAttribute)) == null)
            {
                return(type);
            }

            TypeBuilder typeBuilder;

            if (Inherit)
            {
                if (CacheProxyClass.ContainsKey(implementationType))
                {
                    return(CacheProxyClass[type]);
                }
                typeBuilder = moduleBuilder.DefineType("CZGLAOP." + type.Name + _TypeName, type.Attributes, type);
            }
            else
            {
                if (CacheProxyClass.ContainsKey(implementationType))
                {
                    return(CacheProxyClass[interfaceType]);
                }
                typeBuilder = moduleBuilder.DefineType("CZGLAOP." + type.Name + _TypeName, type.Attributes, type);
            }

            // 判断是否为泛型,如果是则构造其为泛型
            bool isGeneric = EmitHelper.DefineGenericParameters(typeBuilder, type);

            // 生成代理类型
            Type proxyType = ActionInterceptor(type, typeBuilder, Inherit);

            CacheProxyClass.TryAdd(implementationType, proxyType);
            return(proxyType);
        }
Пример #2
0
        /// <summary>
        /// 通过非侵入式来生成代理类型
        /// </summary>
        /// <param name="interfaceType">实现的接口或继承</param>
        /// <param name="implementationType">实现的类型</param>
        /// <param name="Inherit">是否通过继承生成</param>
        /// <returns></returns>
        public static Type CreateProxyClassTypeNoAttribute(Type implementationType, NoActionAttributeModel noAction)
        {
            Type        type = implementationType;
            TypeBuilder typeBuilder;

            if (CacheProxyClass.ContainsKey(implementationType))
            {
                return(CacheProxyClass[type]);
            }
            typeBuilder = moduleBuilder.DefineType("CZGLAOP." + type.Name + _TypeName, type.Attributes, type);

            // 判断是否为泛型,如果是则构造其为泛型
            bool isGeneric = EmitHelper.DefineGenericParameters(typeBuilder, type);

            // 生成代理类型
            Type proxyType = ActionInterceptor(type, typeBuilder, true, noAction);

            CacheProxyClass.TryAdd(implementationType, proxyType);
            return(proxyType);
        }