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
- fixed: #50 and interception example both in tests and docs — committed to dadhi/DryIoc by dadhi 6 years ago
- fixed: Using array as a custom value related to #50 added: Multi-interceptor test to #50 — committed to dadhi/DryIoc by dadhi 6 years ago
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:
use it:
working:
@dadhi Ok, I have a stupid mistake, because when I create a proxy class, my implementation type is not a virtual method.
It is now working properly.
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.Ofselector 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! 😃