在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
重量级的ORM和IOC产品离不开动态代理,作为开发人员,多数情况不用关注动态代理的内部实现机制,但是了解其一般的规律和模式还是有必要的,比如:虽然你开发期间采用了POCO,因为开启了动态代理,运行期间则不是POCO。本文简单描述了5种代理生成模式和1种Mixin模式,最后给出一个示例。 复制代码 代码如下: public interface IPlayable { void Play(); } public class Animal : IPlayable public class Dog : Animal public interface IRunable public class RunAbility : IRunable public class AnimalInterceptor : IInterceptor 第一种:ClassProxy 复制代码 代码如下: { Console.WriteLine("\n*************ClassProxy*************\n"); var generator = new ProxyGenerator(); var animal = generator.CreateClassProxy<Animal>(new AnimalInterceptor()); animal.Play(); Console.WriteLine(animal.GetType()); var compositeField = animal.GetType().GetField("__target"); foreach (var interfaceType in animal.GetType().GetInterfaces()) 第二种:ClassProxyWithTarget 复制代码 代码如下: { Console.WriteLine("\n*************ClassProxyWithTarget*************\n"); var generator = new ProxyGenerator(); var animal = generator.CreateClassProxyWithTarget<Animal>(new Dog(), new AnimalInterceptor()); animal.Play(); Console.WriteLine(animal.GetType()); var compositeField = animal.GetType().GetField("__target"); foreach (var interfaceType in animal.GetType().GetInterfaces()) 第三种:InterfaceProxyWithoutTarget 复制代码 代码如下: { Console.WriteLine("\n*************InterfaceProxyWithoutTarget*************\n"); var generator = new ProxyGenerator(); var animal = generator.CreateInterfaceProxyWithoutTarget<IPlayable>(new AnimalInterceptor()); animal.Play(); Console.WriteLine(animal.GetType()); var compositeField = animal.GetType().GetField("__target"); foreach (var interfaceType in animal.GetType().GetInterfaces()) 第四种:InterfaceProxyWithTarget 复制代码 代码如下: { Console.WriteLine("\n*************InterfaceProxyWithTarget*************\n"); var generator = new ProxyGenerator(); var animal = generator.CreateInterfaceProxyWithTarget<IPlayable>(new Dog(), new AnimalInterceptor()); animal.Play(); Console.WriteLine(animal.GetType()); var compositeField = animal.GetType().GetField("__target"); foreach (var interfaceType in animal.GetType().GetInterfaces()) 第五种:InterfaceProxyWithTargetInterface 复制代码 代码如下: { Console.WriteLine("\n*************InterfaceProxyWithTargetInterface*************\n"); var generator = new ProxyGenerator(); var animal = generator.CreateInterfaceProxyWithTargetInterface<IPlayable>(new Dog(), new AnimalInterceptor()); animal.Play(); Console.WriteLine(animal.GetType()); var compositeField = animal.GetType().GetField("__target"); foreach (var interfaceType in animal.GetType().GetInterfaces()) Mixin模式 复制代码 代码如下: { Console.WriteLine("\n*************Mixin*************\n"); var generator = new ProxyGenerator(); var options = new ProxyGenerationOptions(); options.AddMixinInstance(new RunAbility()); var animal = generator.CreateClassProxy<Animal>(options, new AnimalInterceptor()); animal.Play(); (animal as IRunable).Run(); Console.WriteLine(animal.GetType()); var compositeField = animal.GetType().GetField("__target"); foreach (var field in animal.GetType().GetFields()) foreach (var interfaceType in animal.GetType().GetInterfaces()) |
请发表评论