重量级的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>(new AnimalInterceptor());
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>(new Dog(), new AnimalInterceptor());
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<IPlayable>(new AnimalInterceptor());
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<IPlayable>(new Dog(), new AnimalInterceptor());
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<IPlayable>(new Dog(), new AnimalInterceptor());
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>(options, new AnimalInterceptor());
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);
}
}
动态代理,模式
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。