使用Unity拦截应用程序中的所有类

沙希

我想在我的应用程序中使用拦截器来进行日志记录。当然,我可以向拦截器注册每种类型,如下所示:

container.AddNewExtension<Interception>();
container.RegisterType<ITenantStore, TenantStore>(
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<LoggingInterceptionBehavior>());

但是由于我有很多类型,所以我想知道是否有一种更干净的方法可以对所有班级做到这一点?

泰勒·奥尔森

这是一种可以对所有已注册类型进行拦截的方法。因此,请在注册完成后致电此网站。然后,您可以使用策略注入来连接要使用的拦截器。

public static class UnityContainerInterceptionExtensions
{
    public static IUnityContainer EnableInterceptionForAllRegisteredTypes
        (this IUnityContainer unityContainer)
    {
        unityContainer
            .Registrations
            .ForEach(r => unityContainer.EnableInterception(r.RegisteredType, r.Name));

        return unityContainer;
    }

    public static IUnityContainer EnableInterception<T>
        (this IUnityContainer unityContainer, string name = null)
    {
        return unityContainer.EnableInterception(typeof (T), name);
    }

    public static IUnityContainer EnableInterception
        (this IUnityContainer unityContainer, Type registrationType, string name = null)
    {
        // Don't allow interception of unity types
        if (registrationType.Namespace.StartsWith(typeof(IUnityContainer).Namespace))
            return unityContainer;

        // Don't allow interception on the intercepting call handlers
        if (typeof (ICallHandler).IsAssignableFrom(registrationType))
            return unityContainer;

        var interception = unityContainer.Configure<Interception>();

        var interfaceInterceptor = new InterfaceInterceptor();
        if (interfaceInterceptor.CanIntercept(registrationType))
        {
            interception
                .SetInterceptorFor(registrationType, name, interfaceInterceptor);
            return unityContainer;
        }

        var virtualMethodInterceptor = new VirtualMethodInterceptor();
        if (virtualMethodInterceptor.CanIntercept(registrationType))
        {
            interception
                .SetInterceptorFor(registrationType, name, virtualMethodInterceptor);
            return unityContainer;
        }

        var transparentProxyInterceptor = new TransparentProxyInterceptor();
        if (transparentProxyInterceptor.CanIntercept(registrationType))
        {
            interception
                .SetInterceptorFor(registrationType, name, transparentProxyInterceptor);
            return unityContainer;
        }

        return unityContainer;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

所有应用程序的“拦截会话开始”事件

iOS会拦截我应用程序中的所有网络流量吗?

Unity-使用www加载图像会导致应用程序中的所有图像更改

无法使用AndBug工具查看android应用程序中的所有类

在运行时,找到扩展基类的Java应用程序中的所有类

获取运行Java应用程序时使用的所有类的列表?

有没有办法从网络拦截中隐藏应用程序网络调用

有什么办法可以在Android的Flutter应用程序中拦截“返回”键按下吗?

应用程序由于某个方法中的NullPointerException而崩溃,但正在使用同一类的所有其他方法

在Unity中保存游戏状态的逻辑:应用程序中的每个类都必须具有加载/保存功能?

对整个应用程序Android中的所有选项菜单使用相同的方法

我如何在Gnome 3中的所有应用程序中使用Adwaita Dark?

如何使用Microsoft Graph API在.net核心应用程序中获取所有授权组?

如何在使用try / catch的应用程序中记录所有异常?

为什么不对MVC应用程序中的所有操作使用异步?

如何使用Spring Data替换Spring Boot应用程序中的所有相关实体

使用图谱API从多租户AD应用程序中获取所有用户

如何将数据存储在变量中并在所有应用程序中使用

在 Micronaut 应用程序中列出所有端点时出错(使用 /routes 端点)

对应用程序中的所有活动使用广播接收器

如何使用我的Azure免费帐户列出Graph Explorer中的所有应用程序?

在应用程序的所有活动中重复使用操作栏

如何从 Android 应用程序的所有文件中删除未使用的项目?

捕获所有州的应用程序中的位置

从 azure 活动目录中获取所有应用程序

清除离子应用程序中的所有缓存

记录WinForms应用程序中的所有按钮单击

`sizeof` C ++应用程序中的所有类型

遍历Meteor应用程序中的所有集合