小编Yur*_*oEX的帖子

如何拦截lightinject中的工厂

不知道该怎么做.尝试拦截具有构造函数注入的工厂时,invocationInfo.Proceed()始终失败.

var container = new ServiceContainer();
container.Register<ICool,Cool>();
container.Register<ILogger, Logger>();
container.Register<IInterceptor, LoggingInterceptor>();

//Two problem lines
container.Register<int, IAwesome>((factory, value) => new Awesome(value, factory.GetInstance<ICool>()));
container.Intercept(sr => sr.ServiceType == typeof(IAwesome), sf => sf.GetInstance<IInterceptor>());

var awesome = container.GetInstance<int,IAwesome>(100);
awesome.Yo();
Run Code Online (Sandbox Code Playgroud)

在我的拦截器中失败了这个方法.

public class LoggingInterceptor : IInterceptor
{
    private ILogger _logger;
    public LoggingInterceptor(ILogger logger)
    {
        _logger = logger;
    }
    public object Invoke(IInvocationInfo invocationInfo)
    {
        var returnValue = invocationInfo.Proceed(); //Exception here
        return returnValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

例外:

LightInject.dll中出现"System.InvalidCastException"类型的异常,但未在用户代码中处理

其他信息:无法转换类型的对象"System.Func`1 [ConsoleApplication1.IAwesome]"为类型"System.Object的[]".

抱歉,我无法为Lightinject制作新标签.代表不够:/

c# aop dependency-injection interceptor light-inject

4
推荐指数
1
解决办法
1352
查看次数