windows2003 服务器Project 2007 , 打了 sp2 , VS2010 引用 COM , Microsoft Project 12.0 object library.
VBA:
Application.ActiveProject.Author = "OK"
MsgBox (Application.ActiveProject.Author)
正确.
.Net代码
// pc 是一个 Project 的 Application
pc.ActiveProject.Author = dbr.Person.FindByUserID(this.Creator).Name;
pc.Caption = this.PlanName;
pc.ActiveProject.Title = this.PlanName;
结果, 作者信息显示在了 经理上, 标题信息显示在了 单位 上. 而真正的标题信息显示了文件名 ,如图:
不解! 大家遇到过吗?
答案: 由于是 CLR 4.0 程序 , 所以要将 Project 的COM 的 嵌入互操作类型 改为 false . CLR < 4 的程序无此问题.
发现过程异常艰难.在调试时,
pc.ActiveProject.Title 与 ((Microsoft.Office.Interop.MSProject.ProjectClass)pc.ActiveProject).Title 的值不同. 而在程序里代码里编写 ((Microsoft.Office.Interop.MSProject.ProjectClass)pc.ActiveProject).Title 确提示 无法嵌入互操作类型Microsoft.Office.Interop.MSProject.ProjectClass,请改用适用的接口.
原来, 嵌入的互操作类型,只把(接口, 代理, 枚举, 结构)嵌入到目标程序集, 其它的IL(类,静态方法)不能嵌入.
引用: http://www.pin5i.com/showtopic-23072.html
-----------------------------------------------------------------------------------------------------------------------------
前言 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
[assembly:Guid(…)]
[assembly:ImportedFromTypeLib(…)]
参考文献
Ahme的blog “CLR 4.0: Type Embedding” http://eknowledger.spaces.live.com/blog/cns!F475D4DE444DB1AB!3238.entry 此博文是英文的, 还配有图片, 英文好的同学可以读读.