Dynamic Proxy 5 mode usage examples and mixin mode _ Practical Tips

Source: Internet
Author: User
Tags generator

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





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.