. NET is a multi-language platform, which is well known. Its implementation principle is that it is a code Instruction Platform Based on MSIL's intermediate language. So. NET language compilation is divided into two parts, from language to MSIL compilation I like to call pre-compilation), and runtime from MSIL to local commands, instant compilation JIT ). JIT compilation is divided into economic compilers and general compilers, which are not the focus of this article. This article mainly discusses the changes we can make in the pre-compilation process, and the generated IL. Let's look at some syntactic sugar of Microsoft C #3.0, Static injection of PostSharp, and so on before and after compilation.
1: Let's take a look at the simplest var:
C #:
- public void TestVar()
-
- {
- var i = 0;
- Console.WriteLine(i);
- }
Use Reflector to view the generation
IL:
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1U2434638-0.jpg "/>
Decompiled C #:
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1U2436063-1.jpg "/>
Here VS converts var into int type during compilation.
2: Action <int>:
C #:
- public void TestAction()
-
- {
-
- var i = 1;
-
- Func<int,int> f = t => t+1;
-
- i=10;
-
- f(i);
-
C #:
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1U243K41-2.jpg "/>
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1U2432227-3.jpg "/>
The compiler generates a proxy method here.
Summary:
Lambda expression compilation rules:
When a lambda expression is assigned a delegate type, such as Action <T> or Func <T, TResult>, the lambda expression is directly compiled by the compiler
1) When lambda expressions do not use non-local references in the closure or use this, they are compiled as a private static method;
2) When lambda expression does not use non-local references in the closure, but this is used, it is compiled as a private member method;
3) when a lambda expression references a non-local variable, it is compiled into a private internal class that promotes the referenced non-local variable to an internal class.
3: PostSharp:
PostSharp integrates MSBuild Task and MSIL Injection technology to implement AOP programming through static Injection during compilation. Change the compiling behavior of vs during compilation. For more information, visit the PostSharp website.
Original c #:
- using System;
-
- using System.Collections.Generic;
-
- using System.Linq;
-
- using System.Text;
-
- namespace ConsoleApplication1
-
- {
-
- class Program
-
- {
-
- static void Main(string[] args)
-
- {
-
- new Program().TestPostSharp();
-
- }
-
- [ErrorHandler()]
-
- public void TestPostSharp()
-
- {
-
- throw new Exception("I will throw a exception!");
-
- }
-
- }
-
- [Serializable]
-
- public class ErrorHandlerAttribute : PostSharp.Laos.OnMethodBoundaryAspect
-
- {
-
- public override void OnException(PostSharp.Laos.MethodExecutionEventArgs eventArgs)
-
- {
-
- //do some AOP operation!
-
- Console.WriteLine(eventArgs.Method+":" +eventArgs.Exception.Message);
-
- eventArgs.FlowBehavior = PostSharp.Laos.FlowBehavior.Continue;
-
- }
-
- }
-
- }
After decompilation:
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1U2436262-4.jpg "/>
This is the end of today, just a simple understanding of the IL injection instance, will be used laterMSBuild Task + Mono CecilAndPostSharpImplement some simple injection instances.