5-mode use example and Mixin mode of dynamic proxy

Source: Internet
Author: User

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 );
}
}


 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.