您现在的位置是:网站首页> 编程资料编程资料
动态代理的5模式使用示例和Mixin模式_实用技巧_
2023-05-24
352人已围观
简介 动态代理的5模式使用示例和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);
}
}
![]() |
相关内容
- .NET命令行解析器示例程序(命令行选项功能)_实用技巧_
- .NET实现热插拔功能(动态替换功用)方案实例_实用技巧_
- .net让线程支持超时的方法实例和线程在执行结束后销毁的方法_实用技巧_
- ASP.NET拒绝访问临时目录的解决方法_实用技巧_
- Asp.Net Couchbase Memcached图文安装调用开发_实用技巧_
- 使用Aspose.Cells组件生成Excel文件实例_实用技巧_
- 在Web用户控件中引用样式表中样式的方法_实用技巧_
- Asp.net调试的一些问题小结_实用技巧_
- log4net创建系统日志的详细步骤_实用技巧_
- net insert into语法错误详解_实用技巧_

