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