The heavyweight ORM and IOC products are inseparable from dynamic agents, as a developer, most situations do not pay attention to the internal implementation mechanism of dynamic agents, but it is necessary to understand their general rules and patterns, such as: Although you use the POCO during development, because the dynamic agent opened, the runtime is not Poco. This paper briefly describes 5 kinds of proxy generation modes and 1 mixin modes, and finally gives an example.
Copy Code code as follows:
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");
}
}
The first type: Classproxy
Copy Code code as follows:
{
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);
}
}
The second type: Classproxywithtarget
Copy Code code as follows:
{
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);
}
}
The third type: Interfaceproxywithouttarget
Copy Code code as follows:
{
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);
}
}
The fourth kind: interfaceproxywithtarget
Copy Code code as follows:
{
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);
}
}
The fifth kind: Interfaceproxywithtargetinterface
Copy Code code as follows:
{
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 mode
Copy Code code as follows:
{
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);
}
}