惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
Security Latest
Security Latest
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
H
Help Net Security
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
博客园_首页
S
Securelist
S
Schneier on Security
G
GRAHAM CLULEY
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
有赞技术团队
有赞技术团队
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
量子位
L
Lohrmann on Cybersecurity
Hugging Face - Blog
Hugging Face - Blog
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
P
Privacy & Cybersecurity Law Blog
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
The Hacker News
The Hacker News
罗磊的独立博客
博客园 - 司徒正美
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
L
LINUX DO - 热门话题
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
V2EX
V
Vulnerabilities – Threatpost
AWS News Blog
AWS News Blog
小众软件
小众软件
Project Zero
Project Zero

博客园 - Andrew Yin

数据访问层组件设计以及选型意识流 第四次扩展的讨论 数据访问层组件设计以及选型意识流 第三次封装(极致、极简而不简单) 数据访问层组件设计以及选型意识流 第二次封装以及各种牢骚 数据访问层组件设计以及选型意识流 第一次封装 数据访问层组件设计以及选型意识流 开篇 UTF-8, Unicode, GB2312三种编码方式解析, 深入研究汉字编码 C++中的模板实例:链表模板 一步一步学Ruby系列(二):Ruby中的函数 一步一步学Ruby系列(一):Ruby基础知识 AST,DLR,Expression Tree-----------推荐一位牛人的博客! C# 交互式SHELL C#中的 eval System.Web.Routing命名空间代码解析(四) Route解析中用到的实体类,一些以"Segment”为名的类 System.Web.Routing命名空间代码解析(三) RouteCollection类 System.Web.Routing命名空间代码解析(二) Routing类(上) System.Web.Routing命名空间代码解析(一) RouteBase类,RouteData类,RouteValueDictionary类 ASP.NET MVC中的各种上下文对象 有关string和Cookie的几个有用的扩展方法 反射加异步,根据枚举值来异步执行方法 操作Enum的一些实用方法
.NET下的AOP: PostSharp 原理分析
Andrew Yin · 2008-12-10 · via 博客园 - Andrew Yin

这段时间又在研究AOP了,对各种动态注入原理的AOP已经不感兴趣了,想研究一下静态注入的AOP,首当其冲的是PostSharp。

PS:Routing 和ASP.NET MVC的文章暂时放一放了

所谓静态注入,指的是在编译好的程序集(最好叫装配集)中的类型或者某个方法里注入IL代码(而不是在程序集的运行时注入),注入的地方称为"Join-Point".

有好多工具都可以读写程序集,注入IL代码,如Reflector的一个插件。但是PostSharp是在你编译一个程序集的时候就把IL注入到程序集里去了。

PostSharp并不像ReSharp一样,它不是VS的一个插件,它也不是在编辑器里修改代码的。

那么,PostSharp是如何起作用的呢?

它是怎么把自己插入到编译器的编译生成过程的呢?

我们都知道VS是通过MSBiuld来执行生成过程的,所以其实PostSharp是把自己作为一系列的Build Task来插入到生成过程的。

然后它修改了VS调用MSBiuld的默认配置。从而使VS的Build命令执行了它所指定的Task。下面的图说明了PostSharp的原理:

但是要真正理解静态注入的原理,要理解IL读写,还需要深入了解.net程序集,了解CLI和IL。

要真正知道PostSharp是如何工作的就必须了解MSBuild,知道怎么执行自定义的Build Task.

有很多东西VS都给我们做了,所以我们不了解,有很多开发人员现在还认为.NET 的程序集就是一个托管DLL文件呢!

接下来我将要写一些关于这方面的文章。

不过大家可以在下面的两个链接找到MSBuild的文档:

http://msdn.microsoft.com/zh-cn/library/wea2sca5.aspx

http://msdn.microsoft.com/zh-cn/library/0k6kkbsd.aspx

引用一下PostSharp文档里的一段:

Difference between the PostSharp Code Object Model and System.Reflection

PostSharp uses the entities defined in the specifications of the Common Language Infrastructure (CLI). However, developers are more familiar with the concepts of System.Reflection. They should be aware than System.Reflection includes great simplifications with respect to CLI specifications. CLI is module oriented. A module is an atomic artifact that can bind to other modules to form complete applications. In order to refer to types defined outside the current module, one uses references. By contrast, System.Reflection lives in the application universe where all references between modules are already resolved. In CLI and in PostSharp, references are explicitly represented as separate entities. But in System.Reflection, there is simply no concept of reference.

Modules

Modules (ModuleDeclaration) are atomic artifacts (i.e. they cannot be separated in smaller units). .NET modules are in fact Portable Executables (PE) just like native libraries and executables.

Assemblies

There is a common confusion about assemblies. An assembly, as indicated by its name, is a set of assembled modules and other files. So an assembly can be composed of many modules and even of unmanaged modules! It is a common mistake and misleading to consider that an assembly is composed of a single file.

What defines intrinsically the assembly is its assembly manifest (AssemblyManifestDeclaration). The manifest is composed of a list of references to modules and external assemblies, and of a list of exported types. The assembly manifest is contained in 'principal' managed module of the assembly (and not in a separate XML file, as claims another frequent mistake).

So the assembly manifest (AssemblyManifestDeclaration) is contained in a module (ModuleDeclaration); it contains references to external modules (ModuleRefDeclaration) and external assemblies (AssemblyRefDeclaration).

So let's summarize:

  • Modules, not assemblies, are the real atoms.
  • Assemblies are composed of many modules (managed or unmanaged executables).
  • Assemblies are defined by an assembly manifest.
  • The assembly manifest is stored in a managed module.
  • A managed module may, but is not required to, contain an assembly manifest.

PostSharp makes a difference between an assembly and an assembly manifest. The AssemblyEnvelope class encapsulates the assembly understood as a collection of modules. The AssemblyManifestDeclaration class encapsulates the manifest; it does not contain modules but is contained by a module. Where AssemblyManifestDeclaration contains a collection of references to modules, AssemblyEnvelope contain modules in themselves.

Domains

PostSharp domains (Domain) are similar to the .NET class AppDomain. Domains are contexts in which references between assemblies are solved. Just as in .NET, PostSharp assemblies are always loaded in a domain. PostSharp allows many domains to be loaded paralelly, even in the same AppDomain.

The base of the object tree is the ModuleDeclaration type. Since PostSharp is module-oriented, everything is contained in the module even the assembly manifest.