C# 使用Autofac RuntimeInitialize

C# 使用Autofac RuntimeInitialize,c#,.net,C#,.net,我正在努力做我在postsharp在AutoFac做的事情 Postsharp中的MethodInterceptionSpect类具有RuntimeInitialize方法。如何使用此方法在AutoFac中执行此操作 一切都应该在核心层。我不想在UI层中这样做 Postsharp的缓存方面示例 [Serializable] public class CacheAspect:MethodInterceptionAspect { private Type _cacheType; pr

我正在努力做我在postsharp在AutoFac做的事情

Postsharp中的MethodInterceptionSpect类具有RuntimeInitialize方法。如何使用此方法在AutoFac中执行此操作

一切都应该在核心层。我不想在UI层中这样做

Postsharp的缓存方面示例

[Serializable]
public class CacheAspect:MethodInterceptionAspect
{
    private Type _cacheType;
    private int _cacheByMinute;
    private ICacheManager _cacheManager;

    public CacheAspect(Type cacheType, int cacheByMinute=60)
    {
        _cacheType = cacheType;
        _cacheByMinute = cacheByMinute;
    }
    public override void RuntimeInitialize(MethodBase method)
    {
        if (typeof(ICacheManager).IsAssignableFrom(_cacheType) == false)
        {
            throw new Exception("Wrong Cache Manager");
        }
        _cacheManager = (ICacheManager)Activator.CreateInstance(_cacheType); //reflection c#
        base.RuntimeInitialize(method);
    }
    public override void OnInvoke(MethodInterceptionArgs args)
    {
        ....
        base.OnInvoke(args);
        _cacheManager.Add(key,args.ReturnValue,_cacheByMinute);
    }
}
public class CacheAspect:IInterceptor
{
    private Type _cacheType;
    private int _cacheByMinute;
    private ICacheManager _cacheManager;

    public CacheAspect(Type cacheType, int cacheByMinute = 60)
    {
        _cacheType = cacheType;
        _cacheByMinute = cacheByMinute;
    }
    //public override void RuntimeInitialize(MethodBase method)
    //{
    //    if (typeof(ICacheManager).IsAssignableFrom(_cacheType) == false)
    //    {
    //        throw new Exception("Wrong Cache Manager");
    //    }
    //    _cacheManager = (ICacheManager)Activator.CreateInstance(_cacheType); //reflection c#
    //    base.RuntimeInitialize(method);
    //}
    public void Intercept(IInvocation invocation)
    {

        ...
        invocation.Proceed();

        _cacheManager.Add(key, invocation.ReturnValue, _cacheByMinute);
    }

    
    
}
Postsharp的缓存方面示例

[Serializable]
public class CacheAspect:MethodInterceptionAspect
{
    private Type _cacheType;
    private int _cacheByMinute;
    private ICacheManager _cacheManager;

    public CacheAspect(Type cacheType, int cacheByMinute=60)
    {
        _cacheType = cacheType;
        _cacheByMinute = cacheByMinute;
    }
    public override void RuntimeInitialize(MethodBase method)
    {
        if (typeof(ICacheManager).IsAssignableFrom(_cacheType) == false)
        {
            throw new Exception("Wrong Cache Manager");
        }
        _cacheManager = (ICacheManager)Activator.CreateInstance(_cacheType); //reflection c#
        base.RuntimeInitialize(method);
    }
    public override void OnInvoke(MethodInterceptionArgs args)
    {
        ....
        base.OnInvoke(args);
        _cacheManager.Add(key,args.ReturnValue,_cacheByMinute);
    }
}
public class CacheAspect:IInterceptor
{
    private Type _cacheType;
    private int _cacheByMinute;
    private ICacheManager _cacheManager;

    public CacheAspect(Type cacheType, int cacheByMinute = 60)
    {
        _cacheType = cacheType;
        _cacheByMinute = cacheByMinute;
    }
    //public override void RuntimeInitialize(MethodBase method)
    //{
    //    if (typeof(ICacheManager).IsAssignableFrom(_cacheType) == false)
    //    {
    //        throw new Exception("Wrong Cache Manager");
    //    }
    //    _cacheManager = (ICacheManager)Activator.CreateInstance(_cacheType); //reflection c#
    //    base.RuntimeInitialize(method);
    //}
    public void Intercept(IInvocation invocation)
    {

        ...
        invocation.Proceed();

        _cacheManager.Add(key, invocation.ReturnValue, _cacheByMinute);
    }

    
    
}