seesharper / LightInject.Interception

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

ArgumentException: TypePassedMustBeInterface #31

Open artem-voropaev opened 3 years ago

artem-voropaev commented 3 years ago

In 2.0.3 you use GetRuntimeInterfaceMap to properly map methods in inherited classes, but when target method is System.Object methods

ArgumentException: TypePassedMustBeInterface is thrown. InterfaceMapping interfaceMapping = proxyDefinition.ImplementingType.GetTypeInfo().GetRuntimeInterfaceMap(targetMethod.DeclaringType);

targetMethod.DeclaringType is SystemObject

Here is the test demonstrates this


        [Fact]
        public void Intercept_Service_Throws_TypePassedMustBeInterface()
        {
            var container = new ServiceContainer();
            container.Register<IFoo, Foo>();
            container.Intercept(sr => sr.ServiceType == typeof(IFoo), (sf, pd) => pd.Implement(() => new SampleInterceptor(), m => !m.IsPropertyGetter()));

            var instance = container.GetInstance<IFoo>();

            Assert.IsAssignableFrom<IProxy>(instance);
        }

to bypass this behaviour we should write MethodSelector like this


container.Intercept(sr => sr.ServiceType == typeof(IFoo), (sf, pd) => pd.Implement(() => new SampleInterceptor(), m => !m.IsPropertyGetter() && m.DeclaringType != typeof(object)));