有哪些动态代理模式和Mixin模式-创新互联
这篇文章主要介绍“有哪些动态代理模式和Mixin模式”,在日常操作中,相信很多人在有哪些动态代理模式和Mixin模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”有哪些动态代理模式和Mixin模式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的平乐网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!重量级的ORM和IOC产品离不开动态代理,作为开发人员,多数情况不用关注动态代理的内部实现机制,但是了解其一般的规律和模式还是有必要的,比如:虽然你开发期间采用了POCO,因为开启了动态代理,运行期间则不是POCO。本文简单描述了5种代理生成模式和1种Mixin模式,最后给出一个示例。
复制代码 代码如下:
public interface IPlayable
{
void Play();
}
public class Animal : IPlayable
{
public virtual void Play()
{
Console.WriteLine("Animal.Play");
}
}
public class Dog : Animal
{
public override void Play()
{
Console.WriteLine("Dog.Play");
}
}
public interface IRunable
{
void Run();
}
public class RunAbility : IRunable
{
public void Run()
{
Console.WriteLine("RunAbility.Run");
}
}
public class AnimalInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Before AnimalInterceptor.Intercept");
if (invocation.InvocationTarget != null)
{
invocation.Proceed();
}
Console.WriteLine("After AnimalInterceptor.Intercept");
}
}
第一种:ClassProxy
复制代码 代码如下:
{
Console.WriteLine("\n*************ClassProxy*************\n");
var generator = new ProxyGenerator();
var animal = generator.CreateClassProxy
animal.Play();
Console.WriteLine(animal.GetType());
Console.WriteLine(animal.GetType().BaseType);
var compositeField = animal.GetType().GetField("__target");
Console.WriteLine(compositeField);
foreach (var interfaceType in animal.GetType().GetInterfaces())
{
Console.WriteLine(interfaceType);
}
}
第二种:ClassProxyWithTarget
复制代码 代码如下:
{
Console.WriteLine("\n*************ClassProxyWithTarget*************\n");
var generator = new ProxyGenerator();
var animal = generator.CreateClassProxyWithTarget
animal.Play();
Console.WriteLine(animal.GetType());
Console.WriteLine(animal.GetType().BaseType);
var compositeField = animal.GetType().GetField("__target");
Console.WriteLine(compositeField);
foreach (var interfaceType in animal.GetType().GetInterfaces())
{
Console.WriteLine(interfaceType);
}
}
第三种:InterfaceProxyWithoutTarget
复制代码 代码如下:
{
Console.WriteLine("\n*************InterfaceProxyWithoutTarget*************\n");
var generator = new ProxyGenerator();
var animal = generator.CreateInterfaceProxyWithoutTarget
animal.Play();
Console.WriteLine(animal.GetType());
Console.WriteLine(animal.GetType().BaseType);
var compositeField = animal.GetType().GetField("__target");
Console.WriteLine(compositeField);
foreach (var interfaceType in animal.GetType().GetInterfaces())
{
Console.WriteLine(interfaceType);
}
}
第四种:InterfaceProxyWithTarget
复制代码 代码如下:
{
Console.WriteLine("\n*************InterfaceProxyWithTarget*************\n");
var generator = new ProxyGenerator();
var animal = generator.CreateInterfaceProxyWithTarget
animal.Play();
Console.WriteLine(animal.GetType());
Console.WriteLine(animal.GetType().BaseType);
var compositeField = animal.GetType().GetField("__target");
Console.WriteLine(compositeField);
foreach (var interfaceType in animal.GetType().GetInterfaces())
{
Console.WriteLine(interfaceType);
}
}
第五种:InterfaceProxyWithTargetInterface
复制代码 代码如下:
{
Console.WriteLine("\n*************InterfaceProxyWithTargetInterface*************\n");
var generator = new ProxyGenerator();
var animal = generator.CreateInterfaceProxyWithTargetInterface
animal.Play();
Console.WriteLine(animal.GetType());
Console.WriteLine(animal.GetType().BaseType);
var compositeField = animal.GetType().GetField("__target");
Console.WriteLine(compositeField);
foreach (var interfaceType in animal.GetType().GetInterfaces())
{
Console.WriteLine(interfaceType);
}
}
Mixin模式
复制代码 代码如下:
{
Console.WriteLine("\n*************Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new RunAbility());
var animal = generator.CreateClassProxy
animal.Play();
(animal as IRunable).Run();
Console.WriteLine(animal.GetType());
Console.WriteLine(animal.GetType().BaseType);
var compositeField = animal.GetType().GetField("__target");
Console.WriteLine(compositeField);
foreach (var field in animal.GetType().GetFields())
{
if (field.Name.StartsWith("__mixin"))
{
Console.WriteLine(field);
}
}
foreach (var interfaceType in animal.GetType().GetInterfaces())
{
Console.WriteLine(interfaceType);
}
}
到此,关于“有哪些动态代理模式和Mixin模式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
分享标题:有哪些动态代理模式和Mixin模式-创新互联
文章路径:http://cdiso.cn/article/csjgoi.html