Get rid of the marshalbyrefobject and dynamicproxy Display Interfaces

Source: Internet
Author: User

Recently, I tried to separate the system permission management function. Naturally, I thought of using AOP to intercept existing functions to add user permission verification steps. Generally, realproxy and marshalbyrefobject are used to mark the method to be intercepted through attribute. In fact, there is no problem. However, if it is a webform and you want to intercept the webform methods, there will be a problem. If you do not support multi-class inheritance, marshalbyrefobject will not be available, so you can use dynamicproxy instead, but the problem arises again, the interface cannot be defined for every page. This will make the existingProgramMust be modified. Dynamicproxy itself is quite good, so this display interface is troublesome. If you remove this display interface, you can easily proxy the method like realproxy, (you can also use dynamic in 4.0 to call proxy object members ).

In dynamicproxy, the display interface has two functions: one is to directly implement the interface through emit and create the proxy type; the other is to use the interface to access the proxy members during the call, intelligence prompts can also be found in the IDE. If you can use the proxy type directly as the parent class of the proxy type, everything will be fine.

The method is divided into two steps: 1. Extract the proxy type member signature and directly use the emit generation interface, replacing the display definition interface with the generation interface; 2, when a proxy type is created, the proxy type is used as the parent class. The proxy member must use the virtual ID. (The latest attempt does not generate an interface. You can directly use the parent type of the agent generated by the proxy object type, such as Castle. dynamicproxy ).

Typebuilder. setparent (objtype );//Objtype is the type of the proxy object.//Or you can specify parenttype directly when definetype.Typebuilder =Modulebuilder. definetype (dynamictypename, typeattributes, objtype,Null);

In practice, you can retain the Interface part. For example, if the proxy type is sealed, the object will be used as the parent type, and the interface (with dynamic) or display interface will be implemented at the same time.

Call example

 Public   Class  Computer {  Public   Void  Testdymaticproxy (){ VaR Computer = (Computer) dynamicproxy. securityproxy. newinstance ( New  Computer (); computer. getprice2 (); Dynamic Com = Dynamicproxy. securityproxy. newinstance ( New  Computer (); com. getprice2 ();}  Public   Virtual   Void  Getprice () {console. writeline (  "  Getprice  " );}  Public   Virtual   Void  Getprice2 () {console. writeline (  "  Getprice2  "  );}} 

Http://files.cnblogs.com/heros/DynamicProxy.rar

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.