/// <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)) { IConfigurableObjectDefinition 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); } } }
public T Create <T>(params Aspect[] with) where T : new() { var proxy = new ProxyFactory { ProxyTargetType = true, TargetSource = new InheritanceBasedAopTargetSource(typeof(T)) }; foreach (var aspect in with) { proxy.AddAdvisor(new SpringAspect { Aspect = aspect }); } var proxyTypeBuilder = new InheritanceAopProxyTypeBuilder(proxy); var proxyType = proxyTypeBuilder.BuildProxyType(); return((T)proxyType.GetConstructors()[0].Invoke(new object[] { proxy })); }