seesharper / LightInject.Interception

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

Directly registered classes are not intercepted #29

Open cnshenj opened 5 years ago

cnshenj commented 5 years ago
public class Foo { public string GetName() { return nameof(Foo); } }
public class Interceptor : IInterceptor
{
    public bool Invoked { get; private set; }
    public object Invoke(IInvocationInfo invocationInfo)
    {
        this.Invoked = true;
        return invocationInfo.Proceed();
    }
}

// Directly register a class as service type and implementing type
container.Register<Foo>();
var interceptor = new Interceptor();
container.Intercept(sr => sr.ServiceType == typeof(Foo), _ => interceptor);
var foo = container.GetInstance<Foo>();
foo.GetName();

// Assertion fails
Assert.True(interceptor.Invoked);
seesharper commented 5 years ago

GetName has to be virtual to make that work 👍