Introduction If you are familiar with aspect-oriented programming (AOP), you will know that adding "facets" to your code can make your code clearer and maintainable. But AOP often relies on third-party class libraries or hard-coded. NET features to work. While the benefits of these implementations outweigh their complexities, I'm still looking for an easier way to implement AOP, to try my code more clearly. I moved them out individually and named ASPECTF. Aspect oriented Programming (AOP) Background "facets" refers to things that have the same commonality in the code you write in different parts of the project. It may be a special way of handling exceptions in your code, recording method calls, time processing, re-executing some methods, and so on. If you do not use any of the class libraries that face-cutting programming to do these things, you will leave some very simple and repetitive code in your entire project that will make your code difficult to maintain. For example, some methods in your business logic layer need to be logged, some exceptions need to be processed, some executions need to be timed, database operations need to be retried, and so on. So, maybe you'll write the following code.
Intercept Messages Public class Aopattribute:proxyattribute { publicoverride MarshalByRefObject CreateInstance (Type servertype) { //Aopproxy is the interception message processing new aopproxy (servertype) ; return as MarshalByRefObject; } }
processing Messages Public classAopproxy:realproxy { Publicaopproxy (Type servertype):Base(servertype) {}//How to handle messages after they are intercepted Public OverrideIMessage Invoke (IMessage msg) {if(msg isIConstructionCallMessage)//if it is a constructor, return it in the original way. {iconstructioncallmessage constructcallmsg= Msg asIConstructionCallMessage; IConstructionReturnMessage Constructionreturnmessage= This. Initializeserverobject ((iconstructioncallmessage) msg); Realproxy.setstubdata ( This, Constructionreturnmessage.returnvalue); Console.WriteLine ("Call Constructor"); returnConstructionreturnmessage; } Else //if it is a method call (the property is also one of the method calls){imethodcallmessage callmsg= Msg asIMethodCallMessage; IMessage message; Try { Object[] args =Callmsg.args; //call the method you want to implement here. Objecto =CallMsg.MethodBase.Invoke (Getunwrappedserver (), args); Message=Newreturnmessage (o, args, args. Length, Callmsg.logicalcallcontext, callmsg); } Catch(Exception e) {message=NewReturnMessage (E, callmsg); } Console.WriteLine (Message. properties["__return"]. ToString ()); returnmessage; } } }
called class [Aopattribute] Public classAopclass:contextboundobject { Public stringHello () {return "Welcome"; } Public ObjectGetName (stringname) { returnName +"said: This tree is I planted, this road is I drive, want to live from now on, leave the life"; } }
class program { staticvoid Main (string[] args) { Aopclass ap=New Aopclass (); int 1 ; Ap. Hello (); K+ +; Ap. GetName ("donsgheng"); Console.WriteLine (k); } }
AOP Slicing programming