seesharper / LightInject.Interception

LightInject.Interception supports Aspect Oriented Programming through proxy-based method interceptors.
11 stars 7 forks source link

Intercept methods that have a custom attribute #17

Closed leandro86 closed 6 years ago

leandro86 commented 6 years ago

Hello,

As the title says, is this possible? Here's my code:

public class RequiresTransactionAttribute : Attribute
{
}

public interface IFoo
{
    void DoSomething();
}

public class Foo : IFoo
{
    [RequiresTransaction]
    public void DoSomething()
    {
        // ...
    }
}

container.Register<IFoo, Foo>();
container.Intercept(sr => sr.ServiceType == typeof(IFoo), (sf, pd) => DefineProxyType(sf, pd));

private void DefineProxyType(IServiceFactory sf, ProxyDefinition pd)
{
    pd.Implement(() => sf.GetInstance<IInterceptor>(), mi => mi.GetCustomAttribute<RequiresTransactionAttribute>() != null);
}

This is not working, as I'm getting, in the proxy definition, a reference to the type IFoo (which doesn't have the attribute), instead of the target Foo. If I do this:

public interface IFoo
{
    [RequiresTransaction]   
    void DoSomething();
}

Then the method is intercepted. But I would like to avoid setting the attribute in the interface.

leandro86 commented 6 years ago

Solved in v2.0.2. See https://github.com/seesharper/LightInject.Interception/pull/18.