当前位置: 首页 > news >正文

建设公司网站入账手机网站模板开发工具

建设公司网站入账,手机网站模板开发工具,怎么找货源开网店,做网站学生作业基于特性标注的拦截器注册方式仅限于将拦截器应用到自己定义的类型上#xff0c;对于第三方提供的类型就无能为力了。对于Dora.Interception来说#xff0c;拦截器注册本质上建立拦截器与一个或者多个目标方法之间的映射#xff0c;所以最笨的方式就是利用反射的方式得到表示…基于特性标注的拦截器注册方式仅限于将拦截器应用到自己定义的类型上对于第三方提供的类型就无能为力了。对于Dora.Interception来说拦截器注册本质上建立拦截器与一个或者多个目标方法之间的映射所以最笨的方式就是利用反射的方式得到表示目标方法的MethodInfo对象并将它与对应的拦截器关联在一起。这种方式虽然能够解决问题但是编程体验很差。本篇介绍的基于表达式的拦截器注册方式利用针对目标方法或者属性的调用表达式以一种强类型的方式获取到目标方法极大地改善了编程体验。一、IInterceptorRegistry以表达式采用强类型的方式将指定类型的拦截器应用到目标方法上是借助如下这个IInterceptorRegistry接口完成的。IInterceptorRegistry接口提供了一个ForTInterceptor方法以待注册的拦截器类型关联参数arguments用来提供构建拦截器对象的参数。该方法会返回一个IInterceptorRegistryTInterceptor对象它提供了一系列的方法帮助我们将指定的拦截器应用到指定目标类型通过泛型参数类型TTarget表示相应的方法上。public interface IInterceptorRegistry {IInterceptorRegistryTInterceptor ForTInterceptor(params object[] arguments);... }public interface IInterceptorRegistryTInterceptor {IInterceptorRegistryTInterceptor ToAllMethodsTTarget(int order);IInterceptorRegistryTInterceptor ToMethodTTarget(int order, ExpressionActionTTarget methodCall);IInterceptorRegistryTInterceptor ToMethod(int order, Type targetType, MethodInfo method);IInterceptorRegistryTInterceptor ToGetMethodTTarget(int order, ExpressionFuncTTarget, object? propertyAccessor);IInterceptorRegistryTInterceptor ToSetMethodTTarget(int order, ExpressionFuncTTarget, object? propertyAccessor);IInterceptorRegistryTInterceptor ToPropertyTTarget(int order, ExpressionFuncTTarget, object? propertyAccessor); }封装了IServiceCollection集合的InterceptionBuilder提供了一个RegisterInterceptors扩展方法我们可以利用该方法定义的ActionIInterceptorRegistry类型的参数来使用上述的这个IInterceptorRegistry接口。不论是IServiceCollection接口的BuildInterceptableServiceProvider扩展方法还是IHostBuilder接口的UseInterception方法均提供了一个可选的ActionInterceptionBuilder委托类型的参数。public sealed class InterceptionBuilder {public IServiceCollection Services { get; }public InterceptionBuilder(IServiceCollection services);   }public static class Extensions {public static InterceptionBuilder RegisterInterceptors(this InterceptionBuilder builder, ActionIInterceptorRegistry register);public static IServiceProvider BuildInterceptableServiceProvider(this IServiceCollection services, ActionInterceptionBuilder? setup  null);public static IHostBuilder UseInterception(this IHostBuilder hostBuilder, ActionInterceptionBuilder? setup  null); }二、将拦截器应用到某个类型类似与将InterceptorAttribute标注到某个类型上我们也可以采用这种方式将指定的拦截器应用到目标类型上背后的含义就是应用到该类型可以被拦截的所以方法上含属性方法。public class FoobarInterceptor {public ValueTask InvokeAsync(InvocationContext invocationContext){var method  invocationContext.MethodInfo;Console.WriteLine(${method.DeclaringType!.Name}.{method.Name} is intercepted.);return invocationContext.ProceedAsync();} }public class Foobar {public virtual void M() { }public virtual object? P { get; set; } }我们可以采用如下的方式将调用IInterceptorRegistryTInterceptor的ToAllMethodsTTarget方法将上面定义的拦截器FoobarInterceptor应用到Foobar类型的所有方法上。var foobar  new ServiceCollection().AddSingletonFoobar().BuildInterceptableServiceProvider(interception  interception.RegisterInterceptors(RegisterInterceptors)).GetRequiredServiceFoobar();foobar.M(); foobar.P  null; _  foobar.P;static void RegisterInterceptors(IInterceptorRegistry registry) {var foobar  registry.ForFoobarInterceptor();foobar.ToAllMethodsFoobar(order: 1); }从如下所示的执行结果可以看出Foobar类型的M方法和P属性均被FoobarInterceptor拦截下来源代码。三、应用到指定的方法和属性我们可以通过指定调用方法或者获取属性的表达式来指定拦截器应用的目标方法。我们将目标类型Foobar定义成如下的形式两个重载的M方法和三个属性均是可以拦截的。public class Foobar {public virtual void M(int x, int y) { }public virtual void M(double x, double y) { }public virtual object? P1 { get; set; }public virtual object? P2 { get; set; }public virtual object? P3 { get; set; } }我们利用如下的代码将上面定义的FoobarInterceptor应用到Foobar类型相应的成员上。具体来说我们调用ToMethodTTarget方法应用到两个重载的M方法调用ToPropertyTTarget方法应用到P1属性的Get和Set方法上调用ToGetMethodTTarget和ToSetMethodTTarget方法应用到P2属性的Get方法和P3属性的Set方法。var provider  new ServiceCollection().AddSingletonFoobar().BuildInterceptableServiceProvider(interception  interception.RegisterInterceptors(RegisterInterceptors));var foobar  provider.GetRequiredServiceFoobar();foobar.M(1, 1); foobar.M(3.14, 3.14); foobar.P1  null; _  foobar.P1; foobar.P2  null; _  foobar.P2; foobar.P3  null; _  foobar.P3; Console.ReadLine();static void RegisterInterceptors(IInterceptorRegistry registry) {var foobar  registry.ForFoobarInterceptor();foobar.ToMethodFoobar(order: 1, it  it.M(default(int), default(int))).ToMethodFoobar(order: 1, it  it.M(default(double), default(double))).ToPropertyFoobar(order: 1, it  it.P1).ToGetMethodFoobar(order: 1, it  it.P2).ToSetMethodFoobar(order: 1, it  it.P3); }程序运行后针对Foobar相应成员的拦截体现在如下所示的输出结果上源代码。四、指定构建拦截器的参数如果应用的拦截器类型构造函数指定了参数我们采用这种注册方式的时候也可以指定参数。以如下这个FoobarInterceptor为例其构造函数中指定了两个参数一个是代表拦截器名称的name参数另一个是IFoobar对象。public class FoobarInterceptor {public FoobarInterceptor(string name, IFoobar foobar){Name  name;Foobar  foobar;}public string Name { get; }public IFoobar Foobar { get; }public ValueTask InvokeAsync(InvocationContext invocationContext){Console.WriteLine(${invocationContext.MethodInfo.Name} is intercepted by FoobarInterceptor {Name}.);Console.WriteLine($Foobar is {Foobar.GetType()}.);return invocationContext.ProceedAsync();} } public interface IFoobar { } public class Foo : IFoobar { } public class Bar: IFoobar { }public class Invoker {public virtual void M1() { }public virtual void M2() { } }由于字符串参数name无法从依赖注入容器提取所以在注册FoobarInterceptor是必须显式指定。如果容器能够提供IFoobar对象但是希望指定一个不通过的对象也可以在注册的时候显式指定一个IFoobar对象。我们按照如下的方式将两个不同的FoobarInterceptor对象分别应用到Invoker类型的Invoke1和Invoke2方法上并分别将名称设置为Interceptor1和Interceptor2第二个拦截器还指定了一个Bar对象作为参数容器默认提供的IFoobar对象的类型为Foo。var invoker  new ServiceCollection().AddSingletonInvoker().AddSingletonIFoobar, Foo().BuildInterceptableServiceProvider(interception  interception.RegisterInterceptors(RegisterInterceptors)).GetRequiredServiceInvoker();invoker.M1(); Console.WriteLine(); invoker.M2();static void RegisterInterceptors(IInterceptorRegistry registry) {registry.ForFoobarInterceptor(Interceptor1).ToMethodInvoker(order: 1, it  it.M1());registry.ForFoobarInterceptor(Interceptor2, new Bar()).ToMethodInvoker(order: 1, it  it.M2()); }程序运行之后两个FoobarInterceptor对象的名称和依赖的IFoobar对象的类型以如下的形式输出到控制台上源代码。五、拦截屏蔽除了用来注册指定拦截器的ForTInterceptor方法IInterceptorRegistry接口还定义了如下这些用来屏蔽拦截的SuppressXxx方法。public interface IInterceptorRegistry {IInterceptorRegistryTInterceptor ForTInterceptor(params object[] arguments);IInterceptorRegistry SupressTypeTTarget();IInterceptorRegistry SupressTypes(params Type[] types);IInterceptorRegistry SupressMethodTTarget(ExpressionActionTTarget methodCall);IInterceptorRegistry SupressMethods(params MethodInfo[] methods);IInterceptorRegistry SupressPropertyTTarget(ExpressionFuncTTarget, object? propertyAccessor);IInterceptorRegistry SupressSetMethodTTarget(ExpressionFuncTTarget, object? propertyAccessor);IInterceptorRegistry SupressGetMethodTTarget(ExpressionFuncTTarget, object? propertyAccessor); }我们可以采用如下的方式会将屏蔽掉Foobar类型所有成员的拦截特性虽然拦截器FoobarInterceptor被注册到了这个类型上源代码。var foobar  new ServiceCollection().AddSingletonFoobar().BuildInterceptableServiceProvider(interception  interception.RegisterInterceptors(RegisterInterceptors)).GetRequiredServiceFoobar(); ...static void RegisterInterceptors(IInterceptorRegistry registry) {registry.ForFoobarInterceptor().ToAllMethodsFoobar(order: 1);registry.SupressTypeFoobar(); }下面的程序明确屏蔽掉Foobar类型如下这些方法的拦截能力M方法P1属性的Get和Set方法如果有以及P属性的Get方法源代码。var foobar  new ServiceCollection().AddSingletonFoobar().BuildInterceptableServiceProvider(interception  interception.RegisterInterceptors(RegisterInterceptors)).GetRequiredServiceFoobar();...static void RegisterInterceptors(IInterceptorRegistry registry) {registry.ForFoobarInterceptor().ToAllMethodsFoobar(order: 1);registry.SupressMethodFoobar(itit.M());registry.SupressPropertyFoobar(it  it.P1);registry.SupressGetMethodFoobar(it  it.P2); }六、两个后备方法通过指定调用目标方法或者提取属性的表达式来提供拦截器应用的方法和需要屏蔽的方法提供了较好的编程体验但是能够提供这种强类型编程模式的前提是目标方法或者属性是公共成员。对于受保护protected的方法和属性我们只能使用如下两个后备方法指定代表目标方法的MethodInfo对象。public interface IInterceptorRegistryTInterceptor {IInterceptorRegistryTInterceptor ToMethodsTTarget(int order, params MethodInfo[] methods); }public interface IInterceptorRegistry {IInterceptorRegistry SupressMethods(params MethodInfo[] methods); }
http://www.sadfv.cn/news/393995/

相关文章:

  • phpcms v9网站模板英德网站建设
  • 企业型网站建设咨询电话南京学做网站
  • 做网站哪里接单湘潭网站建设 r磐石网络
  • 网站更换主机国企网站建设会议纪要
  • 网站建设10个基本步骤生物网站建设
  • 邵阳微网站开发lz2v宠物网站设计与制作
  • 个人备案企业网站做汽配外贸是在哪个网站做
  • 哪个网站做外贸用jq和ajax做能登陆注册的一个网站
  • 婚礼网站有哪些沧州网站建设公司
  • 网站只做五周tornado 做网站
  • 网站建设方案行业电商平台都有哪些
  • 做个平台网站怎么做自己做的网站怎么植入erp
  • 网站建设运营的灵魂是上海自助建站官网
  • 中山cms建站站点建立网站的方法
  • 中英双语网站建设合同香河住房和城乡建设局网站
  • 自己开发一个手机网站昆明参差网站
  • 网站价值评估 php加强机关网站内容建设
  • 毕业设计网站可以做什么网页设计师技术水平证书
  • 百科网站开发用mediawiki做的网站
  • 网站开发基础如何做弹幕视频网站
  • 嘉兴房产网站建设网站后台添加文字
  • 网站建设联雅一个做炉石视频的网站
  • 科技网站设计欣赏辽宁省营商建设监督局网站
  • 为您服务网站泰安网站设计
  • 哪些网站可以免费做推广2024年个体工商户年报
  • phpcms 后台修改修改网站备案号青岛房产交易中心官网
  • 公司网站备案网站名称有什么用wordpress怎么升级
  • 网站建设动画代码哪些网站可以兼职做设计
  • 做旅游网站平台合作入驻wordpress商店网页
  • mvc5网站开发之六 管理员网站做优化有什么用吗