There are already too many dynamic interception methods for AOP. Article But there are few implementations of static cross-cutting. Therefore, I would like to provide an implementation idea for the purpose of launching a discussion.
The main idea of implementation is as follows:
The Source code Write a file (for example, log. aspect), and then intercept the new operator (this can be referred to other people's articles, and use proxyattribute to inherit from marshalbyrefobject) or use the factory mode during construction, and return the inheritance class of the object to be generated, here, I use a temporary source file and dynamic compilation in codedom. Basically temporary files Code As follows:
Public Class Aop_target: Target
{
//Insert the code to weave-in (read from *. aspect), for example:
//Public void log (string MSG)
//{
//
//}
}
This implementation is relatively simple, but the efficiency must be relatively poor. I believe that using emit will greatly improve.
Finally, the weave-in method can only be called Using Reflection and invoke. This is probably a big weakness, but on the premise that the C # compiler is fully used, this is probably the only way to do this.