问题
I like to add tracing entering and leaving of every methods in classes. and also trace the arguments when an exception occurs. With PostSharp AOP, I don't need to put it in every method but just apply attributes. But I can't afford PostSharp.
Is there an alternate way to do this? Can Unity3D do that?
I am using log4net to log the message.
回答1:
Yes you can do Aspect Oriented Programming and Interception using Unity.
Unity is a lightweight, extensible dependency injection container with support for instance and type interception.
There is a great article from Dino Esposito: Aspect-Oriented Programming, Interception and Unity 2.0
Interception in Unity 2.0
The core idea of interception in Unity is enabling developers to customize the chain of calls that it takes to invoke a method on an object. In other words, the Unity interception mechanism captures calls being made to configured objects and customizes the behavior of the target objects by adding some extra code before, after or around the regular execution of methods. Interception is essentially an extremely flexible approach to add new behavior to an object at run time without touching its source code and without affecting the behavior of classes in the same inheritance path.
And also there is a great Patterns and Practices article about Interception using Unity. The example code at article performs logging using interception.
来源:https://stackoverflow.com/questions/32150733/alternate-free-to-aop-postsharp-for-method-tracing-and-exception