Пример #1
0
        protected object CreateProxy(AdvisedSupport advisedSupport)
        {
            Type            proxyType     = new InheritanceAopProxyTypeBuilder(advisedSupport).BuildProxyType();
            ConstructorInfo proxyCtorInfo = proxyType.GetConstructor(new Type[] { typeof(IAdvised) });

            ExpressionEvaluator.GetValue(advisedSupport, "Activate()");
            return(((IAopProxy)proxyCtorInfo.Invoke(new object[] { advisedSupport })).GetProxy());
        }
        private void EnhanceConfigurationClasses(IConfigurableListableObjectFactory objectFactory)
        {
            string[] objectNames = objectFactory.GetObjectDefinitionNames();

            foreach (string t in objectNames)
            {
                IObjectDefinition objDef = objectFactory.GetObjectDefinition(t);

                if (((AbstractObjectDefinition)objDef).HasObjectType)
                {
                    if (Attribute.GetCustomAttribute(objDef.ObjectType, typeof(ConfigurationAttribute)) != null)
                    {

                        ProxyFactory proxyFactory = new ProxyFactory();
                        proxyFactory.ProxyTargetAttributes = true;
                        proxyFactory.Interfaces = Type.EmptyTypes;
                        proxyFactory.TargetSource = new ObjectFactoryTargetSource(t, objectFactory);
                        SpringObjectMethodInterceptor methodInterceptor = new SpringObjectMethodInterceptor(objectFactory);
                        proxyFactory.AddAdvice(methodInterceptor);

                        //TODO check type of object isn't infrastructure type.

                        InheritanceAopProxyTypeBuilder iaptb = new InheritanceAopProxyTypeBuilder(proxyFactory);
                        //iaptb.ProxyDeclaredMembersOnly = true; // make configurable.
                        ((IConfigurableObjectDefinition)objDef).ObjectType = iaptb.BuildProxyType();

                        objDef.ConstructorArgumentValues.AddIndexedArgumentValue(objDef.ConstructorArgumentValues.ArgumentCount, proxyFactory);

                    }
                }
            }
        }
        private Type GenerateProxyType(string objectName, IConfigurableListableObjectFactory objectFactory)
        {
            ProxyFactory proxyFactory = new ProxyFactory();
            proxyFactory.ProxyTargetAttributes = true;
            proxyFactory.Interfaces = Type.EmptyTypes;
            proxyFactory.TargetSource = new ObjectFactoryTargetSource(objectName, objectFactory);
            SpringObjectMethodInterceptor methodInterceptor = new SpringObjectMethodInterceptor(objectFactory);
            proxyFactory.AddAdvice(methodInterceptor);

            //TODO check type of object isn't infrastructure type.

            InheritanceAopProxyTypeBuilder iaptb = new InheritanceAopProxyTypeBuilder(proxyFactory);
            //iaptb.ProxyDeclaredMembersOnly = true; // make configurable.
            return iaptb.BuildProxyType();
        }
        /// <summary>
        /// Allows for custom modification of an application context's object definitions.
        /// </summary>
        /// <remarks>
        /// <p>
        /// If the object name matches, replaces the type by a AOP proxied type based on inheritance.
        /// </p>
        /// </remarks>
        public void PostProcessObjectFactory(IConfigurableListableObjectFactory factory)
        {
            IList<string> objectDefinitionNames = factory.GetObjectDefinitionNames();
            for (int i = 0; i < objectDefinitionNames.Count; ++i)
            {
                string name = objectDefinitionNames[i];
                if (IsObjectNameMatch(name))
                {
                    var definition = factory.GetObjectDefinition(name) as IConfigurableObjectDefinition;

                    if (definition == null || IsInfrastructureType(definition.ObjectType, name))
                    {
                        continue;
                    }

                    ProxyFactory pf = CreateProxyFactory(definition.ObjectType, name);

                    InheritanceAopProxyTypeBuilder iaptb = new InheritanceAopProxyTypeBuilder(pf);
                    iaptb.ProxyDeclaredMembersOnly = this.ProxyDeclaredMembersOnly;
                    Type type = iaptb.BuildProxyType();

                    definition.ObjectType = type;
                    definition.ConstructorArgumentValues.AddIndexedArgumentValue(definition.ConstructorArgumentValues.ArgumentCount, pf);
                }
            }
        }