DryIoc: Questions about property / field can not be injected

I have two definitions of tool classes:

    public interface ITool
    {
        void Print();
    }

    public class Tool : ITool
    {
        public void Print()
        {
            Console.WriteLine("I'm Tool.");
        }
    }

    public class DefaultTool : ITool
    {
        public void Print()
        {
            Console.WriteLine("Null.");
        }
    }

Interceptor definition:

    public class TestInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            Console.WriteLine("At Interceptor ...");
        }
    }

I injected the tool class as an attribute, but the parsed behavior is completely different:

    public interface IApplicationImpl
    {
        void UseTool();
    }
    
    public class ApplicationImpl :IApplicationImpl
    {
        public ITool Tool { get; set; }

        public ApplicationImpl()
        {
            Tool = new DefaultTool();
        }
        
        public void UseTool()
        {
            Tool.Print();
        }
    }

I did this test:

    class Program
    {
        static void Main(string[] args)
        {
            var c = new Container(r=>r.WithTrackingDisposableTransients());
            c.Register<ITool,Tool>();
            c.Register<TestInterceptor>();
            c.RegisterMany(new[]{typeof(IApplicationImpl),typeof(ApplicationImpl)},typeof(ApplicationImpl),Reuse.Transient,PropertiesAndFields.Auto);
            c.Intercept<ApplicationImpl,TestInterceptor>();
            
            // no.1
            c.Resolve<IApplicationImpl>().UseTool();
            // no.2
            c.Resolve<ApplicationImpl>().UseTool();
            
            Console.ReadKey();
        }
    }

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 22 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you. I added a new interface, Custom Interceptor, which inherits from IInterceptor. All of my interceptors implement the ICustomInterceptor interface. When I injected the interceptor, I passed the ICustomInterceptor interface and its implementation type. Now he is working normally.

new code:

    public interface IApplicationImpl
    {
        void UseTool();
    }
    
    public class ApplicationImpl :IApplicationImpl
    {
        public void UseTool()
        {
            Console.WriteLine("I'm ApplicationImpl.");
        }
    }

    public class TestInterceptor : ICustomInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            invocation.Proceed();
            Console.WriteLine("At Interceptor 1...");
        }
    }

    public class TestInterceptor2 : ICustomInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            invocation.Proceed();
            Console.WriteLine("At Interceptor 2...");
        }
    }
    
    public class TestInterceptor3 : ICustomInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            var unproxiedType = invocation.TargetType;
            
            invocation.Proceed();
            Console.WriteLine("At Interceptor 3...");
        }
    }

    public interface ICustomInterceptor : IInterceptor
    {
        
    }

use it:

    class Program
    {
        static void Main(string[] args)
        {
            var c = new Container(r=>r.WithTrackingDisposableTransients());
            c.Register<ICustomInterceptor,TestInterceptor>();
            c.Register<ICustomInterceptor,TestInterceptor2>();
            c.Register<ICustomInterceptor,TestInterceptor3>();
            
            c.RegisterMany(new[]{typeof(IApplicationImpl),typeof(ApplicationImpl)},typeof(ApplicationImpl),Reuse.Transient,PropertiesAndFields.Auto);
            
            c.Intercept<IApplicationImpl,ICustomInterceptor>();
            
            c.Resolve<IApplicationImpl>().UseTool();

            Console.ReadKey();
        }
    }

working: tim 20181116144311

@dadhi Ok, I have a stupid mistake, because when I create a proxy class, my implementation type is not a virtual method. tim 20181115142333

tim 20181115142454

It is now working properly. image

This exception is due an actual bug in processing of custom value for parameter selector, where value is array. Fix is in progress.

I will probably generalize the API, allowing to pass the Parameter.Of selector for interceptors in most common form. Then the current API will be just a sugar on top.

And probably it is time to release it as separate package. More packages… yahhh! 😃