Introduction to dynamic proxy dynamicproxy

Source: Internet
Author: User
In the previous article When the generic parameter type is dynamic... A simple example shows a dynamic proxy. We use a dynamic proxy, mainly because the dynamic proxy has the ability to make a certain type A Can be converted to a specified interface during running I , Even if this type A The specified interface is not included in the definition. I Inheritance. What does this sentence mean? Back When the generic parameter type is dynamic... Article In the example, List <> Not from Isimplelist Inheritance, but on the surface, we can use dynamic proxy Isimplelist Interface " Reference "List <> Type object.

Dynamic proxy is a type built in the memory at runtime, which implements the interface I But it forwards all method calls to the type A .

 Note: I used "method" call forwarding. Because the events and attributes are all variants of the method, the call of all elements defined in the interface can be dynamically forwarded by the proxy. Then, how does the method in Interface (I) match the method of the proxy (target? The common method is to perform a match with the same name. For example, the Add method of the isimplelist interface naturally matches the add method of list <>. For complex requirements, you can define a method name ing table to match different names.

Because dynamic proxy has this type (target)"Face changeTherefore, it can be used in many occasions to elegantly solve some previously hard-to-solve problems (usually we used reflection to solve these problems), such:
(1) "the generic parameter type is dynamic". Using Dynamic proxy to solve this problem can not only avoid performance loss caused by reflection, but also benefit from calling a strongly typed method.
(2) face changing for a group of types ". For example, Textbox, RichTextBox, listview, and other Windows controls all have the clear method, but none of them implement a unified interface (for example, this interface defines the clear method ), therefore, when I want to clear the content of all controls in a groupbox, I cannot call them in a unified way. You cannot do this:

Foreach (Control In   This . Groupbox1.controls)
{
Control. Clear (); // The clear method does not exist in control. An error is reported during compilation.
}

But with the dynamic proxy, we can define a new face for these controls:

  Public   Interface  Inewface
{
Void Clear ();
}

Then the call is elegantly as follows:

Foreach (Control In   This . Groupbox1.controls)
{
InewfaceFace =  Dynamictypeemitter. Createdynamicproxy < Inewface > (Control );
Face. Clear ();
}

(3) In addition to forwarding calls, dynamic proxies can be used to inject preprocessing and post-processing. This is the function of AOP. Spring. Net's AOP implementation is achieved through dynamic proxy.

You can continue to explore other scenarios where dynamic proxy is used and use your imagination to show the power of dynamic proxy.

esbasic. dll can be used from when the generic parameter type is dynamic ... .

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.