I am trying to do some attribute-based interception using the structure structure, but I'm struggling to tie the last free ends.
I have a custom registry that scans my assemblies and in this registry I defined the following ITypeInterceptor, the purpose of which is to match the types decorated with this attribute, and then apply an interceptor if they match. A class is defined as such:
public class AttributeMatchTypeInterceptor<TAttribute, TInterceptor> : TypeInterceptor where TAttribute : Attribute where TInterceptor : IInterceptor { private readonly ProxyGenerator m_proxyGeneration = new ProxyGenerator(); public object Process(object target, IContext context) { return m_proxyGeneration.CreateInterfaceProxyWithTarget(target, ObjectFactory.GetInstance<TInterceptor>()); } public bool MatchesType(Type type) { return type.GetCustomAttributes(typeof (TAttribute), true).Length > 0; } }
I use DynamicProxy from Castle.Core to create interceptors, but my problem is that the object returned from the call to CreateInterfaceProxyWithTarget (...) does not implement the interface that caused the creation of the target instance in the map structure (for example, IOrderProcessor in the example above) . I was hoping the IContext parameter would open this interface, but I can only seem to grab onto a specific type (i.e. the OrderProcessor in the example above).
I am looking for guidance on working with this scenario by calling ProxyGenerator to return an instance that implements all the interfaces as the target instance, by retrieving the requested interface from the structure structure or using some other mechanism.
source share