Introduction to. NET injection during compilation (C # -- & gt; IL)

Source: Internet
Author: User
Tags microsoft c

. 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 #:

 
 
  1. public void TestVar()  
  2.  
  3. {   
  4.    var i = 0;  
  5.    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 #:

 
 
  1. public void TestAction()  
  2.  
  3. {  
  4.  
  5. var i = 1;  
  6.  
  7. Func<int,int> f = t => t+1;  
  8.  
  9. i=10;  
  10.  
  11. f(i);  
  12.  

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 #:

 

 
 
  1. using System;  
  2.  
  3. using System.Collections.Generic;  
  4.  
  5. using System.Linq;  
  6.  
  7. using System.Text;  
  8.  
  9. namespace ConsoleApplication1  
  10.  
  11. {  
  12.  
  13. class Program  
  14.  
  15. {  
  16.  
  17. static void Main(string[] args)  
  18.  
  19. {  
  20.  
  21. new Program().TestPostSharp();  
  22.  
  23. }  
  24.  
  25. [ErrorHandler()]  
  26.  
  27. public void TestPostSharp()  
  28.  
  29. {  
  30.  
  31. throw new Exception("I will throw a exception!");  
  32.  
  33. }  
  34.  
  35. }  
  36.  
  37. [Serializable]  
  38.  
  39. public class ErrorHandlerAttribute : PostSharp.Laos.OnMethodBoundaryAspect  
  40.  
  41. {  
  42.  
  43. public override void OnException(PostSharp.Laos.MethodExecutionEventArgs eventArgs)  
  44.  
  45. {  
  46.  
  47. //do some AOP operation!  
  48.  
  49. Console.WriteLine(eventArgs.Method+":" +eventArgs.Exception.Message);  
  50.  
  51. eventArgs.FlowBehavior = PostSharp.Laos.FlowBehavior.Continue;  
  52.  
  53. }  
  54.  
  55. }  
  56.  

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.