CLR 4.0 有哪些新东西? What's new in CLR 4.0--类型嵌入 Type embedding-阿里云开发者社区

开发者社区> 开发与运维> 正文

CLR 4.0 有哪些新东西? What's new in CLR 4.0--类型嵌入 Type embedding

简介: 前言 Foreword 自从2005年CLR 2.0发布之后, Microsoft有几年没有更新CLR了. 现在新的版本CLR 4.0即将发布. 本人看了几个英文博文. 综合起来, 摘录其要点. 用一个系列介绍几个CLR4.

前言 Foreword

自从2005年CLR 2.0发布之后, Microsoft有几年没有更新CLR了. 现在新的版本CLR 4.0即将发布. 本人看了几个英文博文. 综合起来, 摘录其要点. 用一个系列介绍几个CLR4.0的新特性. 为照顾英文不好的同学, 特用中英文对照的形式.

Microsoft has not upgraded CLR for years since it released CLR 2.0 in 2005. The new CLR 4.0 is about to release. I read English posts on internet. Do a summary and grab the important points in the English posts. I am going to make a series of posts about what’s new in CLR 4.0. The post is talking about “Type embedding”.

类型嵌入 Type embedding

过去我们用.net开发,部署Office应用时, 我们必须引入Office PIA(Primary interop assembly). 当部署之时, 我们必须在目标机器上安装Office PIA. 虽然Office PIA 体积不大(6M多), 但是这给我们多少带来一些不便.现在CLR4.0的类型嵌入特性给我们提供了一些方便.下面来比较一下: 过去我们开发Office应用, 需要加入Office PIA的引用, 编译后你的Bin目录会有你的程序集和Office PIA的程序集. 当你的应用程序运行之时,你会发现你的应用程序装入了Office PIA的程序集. 在CLR4.0中, 你可以通知CLR将你引用的那些Office PIA的类型嵌入到你的程序集中. 这样在部署之时, Office PIA的程序集就不必一同部署到目标机器上. 具体怎么做呢?

When we develop Office applications using .net in the past, we need to add references to the assemblies of Office PIA. When we deploy the applicaitons, we have to install Office PIA also on the target machines. The size of Office PIA is not so big(about 6M), but it still not convenient enough. Now CLR 4.0 represents us a feature called “Type embedding”. This will provides us conveniency. Let’s work through how we develop Office applications in previous versions of CLR. First step, we need to add reference to Office PIA assemblies, after compiling there are Office PIA assemblies in your bin folder along with your application’s assemblies. If you try to debug your application, you will find your application assembly loads Office PIA assemblies in memory also. In CLR 4.0, you can send a note to CLR that CLR only embeds the types your applicaiton referencing into your assembly. This way you do not need install Office PIA on target machines together with your applicaiton. How to do this?

首先在Visual Studio 2010中加入Office PIA的引用(这和过去一样), 然后在展开引用(References)节点, 找到Office PIA的引用, 在属性(Properties)窗口, 有一个”嵌入互操作类型”(Embed Interop Types), 将其设置成真(True), 再重新编译.

First step you need to add reference to Office PIA assemblies in Visual studio 2010(This is the same as what we did in the past), and then extend the “References” node of your project, click the Office PIA assembly, in “Properties” window, set “Embed interop Types” to True, and compile again.

CLR只将你引用到的类型放入你的程序集中, 同时在你引用到的类型之下, CLR只将你用到的方法和成员以原名称存入你的程序集. 那些未调用的方法将以一个_VtblGap调用来代替, 这些方法的名称将变成以_VtblGap开头的名字.

The CLR rips only the types necessary to complete the calls you have made from your application, and CLR extracts only the methods you have called and replaced all the other methods and Type members with the magic _VblGap calls. Those _VtblGap pseudo methods are emitted in place of unused methods to maintain vtable compatibility.

类型嵌入的限制 Limitations

不能嵌入IL (Can’t embed IL, meaning no classes or static method)

只有元数据能嵌入(接口, 代理, 枚举, 结构) Only metadata is locally embedded (interfaces, delegates, enums, structs )

只有从COM得到的类型才能嵌入. 并且编译器检查这些标签. Only types from Interop Assemblies can be embedded. Compilers check for these attributes

a. [assembly:Guid(…)]

b. [assembly:ImportedFromTypeLib(…)]

参考文献

Ahme的blog  “CLR 4.0: Type Embedding”   http://eknowledger.spaces.live.com/blog/cns!F475D4DE444DB1AB!3238.entry     此博文是英文的, 还配有图片, 英文好的同学可以读读.

mikelij 于 2009.3.8. 23:09

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章