重量級的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);
}
}