Esempio n. 1
0
        /// <summary>
        /// 自动装配属性
        /// <para>为属性对象启用代理,并延迟初始化被代理的对象</para>
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static void AutowiredProperties(object obj)
        {
            if (obj == null)
            {
                return;
            }

            // 获取公共实例属性
            PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

            if (infos != null && infos.Length > 0)
            {
                foreach (PropertyInfo pInfo in infos)
                {
                    AutowiredAttribute autoProxy = ReflectionUtil.GetCustomAttribute <AutowiredAttribute>(pInfo);

                    if (autoProxy == null)
                    {
                        continue;
                    }

                    object pValue = DelayProxyUtil.CreateProxy(pInfo.PropertyType, autoProxy);

                    pInfo.SetValue(obj, pValue, null);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 为指定类型 创建代理
        /// </summary>
        /// <returns></returns>
        public static object CreateProxy(Type type, AutowiredAttribute autoProxy)
        {
            // 为属性对象启用代理,并延迟初始化被代理的对象
            if (autoProxy.UseProxy)
            {
                return(DelayProxyUtil.GetTransparentProxy(type, null, true));
            }

            // 不启用代理,并不延迟初始化
            object instance = Activator.CreateInstance(type);

            // 自动装配属性
            // 为属性对象启用代理,并延迟初始化被代理的对象
            DelayProxyUtil.AutowiredProperties(instance);

            return(instance);
        }