Heavyweight ORM and IOC products cannot be separated from dynamic proxies. as developers, they do not need to pay attention to the internal implementation mechanism of dynamic proxies in most cases. However, it is necessary to understand the general rules and modes, such: although POCO is used during development, it is not POCO during running because dynamic proxy is enabled. This article briefly describes five proxy generation modes and one Mixin mode, and finally provides an example.
Copy codeThe Code is 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 ");
}
}
First: ClassProxy
Copy codeThe Code is 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 );
}
}
Type 2: ClassProxyWithTarget
Copy codeThe Code is 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 );
}
}
Third: InterfaceProxyWithoutTarget
Copy codeThe Code is 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 );
}
}
Fourth: InterfaceProxyWithTarget
Copy codeThe Code is 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 );
}
}
Fifth: InterfaceProxyWithTargetInterface
Copy codeThe Code is 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 codeThe Code is 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 );
}
}