1:我们先来看看最简单的var:
C#:
public void TestVar() { var i = 0; Console.WriteLine(i); }
使用Reflector查看生成
IL:
反编译后的C#:
这里VS在编译的时候将var为我们转变为了int类型。
2:Action
C#:
public void TestAction() { var i = 1; Func
反编译后C#:
编译器为我们在这里生成了代理方法。
总结:
关于lambda表达式的编译规则:
当一个lambda expression被赋值给一个delegate类型,例如Action
3:PostSharp:
PostSharp是结合了 MSBuild Task 和 MSIL Injection 技术,编译时静态注入实现 AOP 编程。在编译时候改变VS的编译行为。更详细的信息,请访问 PostSharp 网站
原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 _disibledevent=>//do some AOP operation! Console.WriteLine(eventArgs.Method+":" +eventArgs.Exception.Message); eventArgs.FlowBehavior = PostSharp.Laos.FlowBehavior.Continue; } } }
反编译后:
今天就到此为至,只是简单的了解下IL注入实例,在后面会利用MSBuild Task+Mono Cecil 和PostSharp实现一些简单的注入实例.
最新评论