手机版

动态代理的5模式使用示例和混合模式

时间:2021-11-13 来源:互联网 编辑:宝哥软件园 浏览:

重量级的ORM和国际奥林匹克委员会产品离不开动态代理,作为开发人员,多数情况不用关注动态代理的内部实现机制,但是了解其一般的规律和模式还是有必要的,比如:虽然你开发期间采用了POCO,因为开启了动态代理,运行期间则不是概念证明。本文简单描述了5种代理生成模式和一种混合模式,最后给出一个示例。复制代码代码如下:公共接口IPlayable { void Play();}公共类Animal : IPlayable {公共虚拟虚空播放(){控制台.写线('动物播放');} }公共类狗:动物公共覆盖无效播放(){控制台.写线('狗播放');} }公共接口我无法{ void Run();}公共类运行能力:无法{公共void Run() {控制台.写线('运行能力.run’);} }公共类animalinterceptor : iintertceptor { public void interceptor(iin职业调用){控制台.写线('在动物感受器之前。截取');如果(调用InvocationTarget!=null){ 0调用。继续();}控制台。写线('在动物感受器之后。截取');} }第一种:ClassProxy复制代码代码如下:{控制台写入行(' \ n * * * * * * * *类代理* * * * * * * * * * * * * * \ n ');var生成器=新的代理生成器();定义变量动物=发电机CreateClassProxyAnimal(new animalinterceptor());动物play();控制台WriteLine(动物. GetType());控制台WriteLine(动物GetType().BaseType);var复合字段=动物GetType().GetField(' _ _ target ');控制台WriteLine(复合字段);foreach(动物中的变种接口类型GetType().GetInterfaces()) { Console .WriteLine(接口类型);} }

第二种:ClassProxyWithTarget复制代码代码如下:{控制台写入行(' \ n * * * * * * * * * * * * *类proxywithttarget * * * * * * * * * \ n ');var生成器=新的代理生成器();定义变量动物=发电机createclassproxywithttargetanimal(新的Dog(),新的animalinterceptor());动物play();控制台WriteLine(动物. GetType());控制台WriteLine(动物GetType().BaseType);var复合字段=动物GetType().GetField(' _ _ target ');控制台WriteLine(复合字段);foreach(动物中的变种接口类型GetType().GetInterfaces()) { Console .WriteLine(接口类型);} }

第三种:InterfaceProxyWithoutTarget复制代码代码如下:{控制台写入行(' \ n * * * * * * * * * * * * * interfaceproxywithoutarget * * * * * * * * * * \ n ');var生成器=新的代理生成器();定义变量动物=发电机createinterfaceproxywithoutargetplayable(新的animalinterceptor());动物play();控制台WriteLine(动物. GetType());控制台WriteLine(动物GetType().BaseType);var复合字段=动物GetType().GetField(' _ _ target ');控制台WriteLine(复合字段);foreach(动物中的变种接口类型GetType().GetInterfaces()) { Console .WriteLine(接口类型);} }

第四种:InterfaceProxyWithTarget复制代码代码如下:{控制台写入行(' \ n * * * * * * * * * * * * * interfaceproxywithttarget * * * * * * * * \ n ');var生成器=新的代理生成器();定义变量动物=发电机CreateInterfaceProxyWithTargetIPlayable(新的Dog(),新的animalinterceptor());动物play();控制台WriteLine(动物. GetType());控制台WriteLine(动物GetType().BaseType);var compositeField=动物GetType().GetField(' _ _ target ');控制台WriteLine(复合字段);foreach(动物中的变种接口类型GetType().GetInterfaces()) { Console .WriteLine(接口类型);} }

第五种:InterfaceProxyWithTargetInterface复制代码代码如下:{控制台写入行(' \ n * * * * * * * * * * * * * interfaceproxywithttargetterface * * * * * * * * \ n ');var生成器=新的代理生成器();定义变量动物=发电机createinterfaceproxywithttargettinterfaceiplayable(新狗(),新animalinterceptor());动物play();控制台WriteLine(动物. GetType());控制台WriteLine(动物GetType().BaseType);var复合字段=动物GetType().GetField(' _ _ target ');控制台WriteLine(复合字段);foreach(动物中的变种接口类型GetType().GetInterfaces()) { Console .WriteLine(接口类型);} }

混合模式复制代码代码如下:{控制台写入行(' \ n * * * * * * * * * * * * * Mixin * * * * * * * * * * * * \ n ');var生成器=新的代理生成器();var options=new ProxyGenerationOptions();选项. AddMixinInstance(新的run ability());定义变量动物=发电机CreateClassProxyAnimal(选项,新的animalinterceptor());动物play();(动物名为无法).run();控制台WriteLine(动物. GetType());控制台WriteLine(动物GetType().BaseType);var复合字段=动物GetType().GetField(' _ _ target ');控制台WriteLine(复合字段);动物的各种领域GetType().GetFields()) { if (field .名称。以(' _ _ mixin '){ Console }开头.WriteLine(字段);} } foreach(动物中的var接口类型.GetType().GetInterfaces()) { Console .WriteLine(接口类型);} }

版权声明:动态代理的5模式使用示例和混合模式是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。