Introduction of dynamic Agent Dynamicproxy

Source: Internet
Author: User

We use dynamic proxies primarily because dynamic proxies have the ability to convert a type A to a specified interface I when it is run, even though this type a does not inherit from this specified interface I at the time of definition. What is the meaning of this remark? or back to when the parameter type of the generic type is dynamic ... The example in the article, in the example,list<> did not inherit from the ISimpleList, but from the surface, in the runtime, we can through the dynamic agent to ISimpleList interface to "reference" list<> type of object.

A dynamic proxy is a type built in memory at run time that implements interface I, but it sends all method calls to type A.

Notice that I used the forwarding of the "method" call, because the events and properties are variations of the method, so calls to all elements defined in the interface can be forwarded by the dynamic proxy. So how does the method in the interface (I) match the method of the agent (Target)? The usual approach is to match "identically named", such as the Add method of the ISimpleList interface, which is naturally matched to the list<> Add method. For complex requirements, you can define a method name Mapping table to match the methods of different names.

Because dynamic proxies have this type of "face-changing" capability, they can be used on many occasions to gracefully address some previously intractable problems (usually, we used reflection to solve the problem), such as:

(1) "Generic parameter types are dynamic", using dynamic proxies to solve this problem can not only avoid the performance loss caused by reflection, but also gain the benefit of strongly typed method invocation.

(2) "Face" for a group of types. For example, Windows controls such as TextBox, RichTextBox, and ListView have a clear method, but neither of them implements a unified interface (for example, the clear method is defined in the interface). So when I want to empty the contents of all the controls in a GroupBox, I can't invoke them in a uniform way, you can't do this:

The following are the referenced contents:
foreach (Control in This.groupBox1.Controls)
{
Control. Clear (); Control does not exist a clear method, compile an error
}

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

The following are the referenced contents:

public interface Inewface
{
void Clear ();
}

And then gracefully call this:

The following are the referenced contents:

foreach (Control in This.groupBox1.Controls)
{
Inewface face = dynamictypeemitter.createdynamicproxy<inewface> (control);
Face. Clear ();
}

You can continue to tap into other situations where you use dynamic proxies, and play your imagination to show the power of dynamic proxies.

One drawback to the Dynamictypeemitter implementation in Esbasic is that it does not support call forwarding for generic methods at the moment, and I don't know how to use emit to launch a call to a generic method of a dynamic type parameter, with the knowledge of a friend please guide.



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.