如何在激活时获取autofac组件的构造函数参数

问题描述 投票:0回答:1

我想要的是为所有用一些属性装饰的组件创建缓存代理。所以,我制作了这样的Autofac模块:

public class CachingModule : Autofac.Module
{
    private readonly ProxyGenerator generator;

    public CachingModule()
    {
        generator = new ProxyGenerator();
    }

    protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
    {
        var type = registration.Activator.LimitType;

        if (type.GetCustomAttribute<CachedAttribute>(true) != null
            || type.GetMethods().Any(m => m.GetCustomAttribute<CachedAttribute>(true) != null))
        {
            registration.Activating += (s, e) =>
            {

                var proxy = generator.CreateClassProxyWithTarget(e.Instance.GetType(),
                    e.Instance,
                    interceptors: e.Context.Resolve<IEnumerable<CacheInterceptor>>().ToArray());

                e.ReplaceInstance(proxy);
            };
        }
    }
}

我无法工作的是:我无法使用参数化构造函数创建代理实例,有没有办法做到这一点?

autofac autofac-module
1个回答
0
投票

好吧,我已经设法让它全部工作,所以对于任何感兴趣的人都是使用非默认构造函数的代理生成示例

public class CachingModule : Autofac.Module
{
    private readonly ProxyGenerator generator;

    public CachingModule()
    {
        generator = new ProxyGenerator();
    }

    protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
    {
        var type = registration.Activator.LimitType;

        if (type.GetCustomAttribute<CachedAttribute>(true) != null
            || type.GetMethods().Any(m => m.GetCustomAttribute<CachedAttribute>(true) != null))
        {
            var ctors = type.GetConstructors();
            registration.Activating += (s, e) =>
            {
                if (e.Instance is IProxyTargetAccessor)
                {
                    return;
                }

                var param = new List<object>();
                var infos = ctors.First().GetParameters();
                if (ctors.Length > 0 && infos.Length > 0)
                {
                    foreach (var p in e.Parameters)
                    {
                        foreach (var info in infos)
                        {
                            if (p.CanSupplyValue(info, e.Context, out var valueProvider))
                            {
                                param.Add(valueProvider());
                            }
                        }
                    }
                }

                var instance = e.Instance;
                var toProxy = instance.GetType();

                var proxyGenerationOptions = new ProxyGenerationOptions(new CacheProxyGenerationHook());

                var proxy = generator.CreateClassProxyWithTarget(toProxy,
                    instance,
                    proxyGenerationOptions,
                    param.ToArray(),
                    interceptors: e.Context.Resolve<IEnumerable<CacheInterceptor>>().ToArray());

                e.ReplaceInstance(proxy);
            };
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.