C# castle 拦截器

 

拦接器

public class AutoDesensitizationMethodInterceptor : IMethodInterceptor
    {
        public object Intercept(IMethodInvocation invocation, params object[] args)
        {
            MethodInfo methodInvocationTarget = invocation.MethodInvocationTarget;
            object obj = invocation.Proceed(args);
            return AttributeVerification.VerificationDesensitization<Object>(obj);
        }
    }
View Code

拦接器

public class AutoDesensitizationInspector : IContributeComponentModelConstruction
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="kernel"></param>
        /// <param name="model"></param>
        // Token: 0x0600048E RID: 1166 RVA: 0x00010344 File Offset: 0x0000E544
        public void ProcessModel(IKernel kernel, ComponentModel model)
        {
            //ICacheFactory autoCacheManager = kernel[typeof(IAutoCacheManager)] as IAutoCacheManager;
            foreach (MethodInfo m in model.Implementation.GetMethods()){
                if (m.IsDefined(typeof(DesensitizationMethodAttribute), true))
                {
                    model.Interceptors.Add(new InterceptorReference(typeof(AutoDesensitizationMethodInterceptor)));
                }
            }
        }
View Code

 

public class AutoDesensitizationFacility : AbstractFacility
    {

        protected override void Init()
        {
            base.Kernel.ComponentModelBuilder.AddContributor(new AutoDesensitizationInspector());
            base.Kernel.AddComponent("AutoDesensitization", typeof(AutoDesensitizationMethodInterceptor));
        }
    }
View Code

 

配置web.config

<castle>
    <facilities>
      <facility id="AutoDesensitization" type="命名空间.AutoDesensitizationFacility, 程序集" />
    </facilities>
</castle>

 

posted @ 2021-05-20 19:51  三瑞  阅读(297)  评论(0编辑  收藏  举报