PostSharp配置项目编译文件

什么是PostSharp?

PostSharp升级到2.0之后不在完全免费。此文针对PostSharp 1.5。(2.0应该大同小异,没有测试,请各位自行测试。)

PostSharp安装之后在本地有详尽的文档。鉴于快餐文化和不要重复造轮子的思想,分享一下如何对指定的项目让PostSharp切入编译。

于此相关的非正常编译信息有:

I

“libs\PostSharp\PostSharp-1.5.targets(91,5): warning : For side-by-side compatibility, the reference to assembly PostSharp.Public should be version-specific.”

II

“warning MSB3245: Could not resolve this reference. Could not locate the assembly "PostSharp.Core".”(或其他的PostSharp.XXX)

也许还有其他的。我没遇到过,请谅解不能一一列举。

主要步骤:

1、XCOPY相关的PostSharp文件到引用目录。文件下载文后。本着宁滥勿缺的态度,文后的文件应该是有多无少,一共也就390k。(此处提供的文件是基于X64安装包的文件,或许X86也可以用,请自行测试。)

2、修改项目文件:UrProject.csproj 或 UrProject.vbcsproj

1)、添加项目引用,此处可以直接在IDE的References中添加。项目的文件的<ItemGroup>节点下,应该有如下的几个Reference

 1 <ItemGroup>
2 <!-- any other references -->
3 <Reference Include="PostSharp.Core, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7">
4 <HintPath>..\libs\PostSharp\PostSharp.Core.dll</HintPath>
5 </Reference>
6 <Reference Include="PostSharp.Laos, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
7 <SpecificVersion>False</SpecificVersion>
8 <HintPath>..\libs\PostSharp\PostSharp.Laos.dll</HintPath>
9 </Reference>
10 <Reference Include="PostSharp.Laos.Weaver">
11 <HintPath>..\libs\PostSharp\PostSharp.Laos.Weaver.dll</HintPath>
12 </Reference>
13 <Reference Include="PostSharp.Public, Version=1.5.0.0,Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
14 <SpecificVersion>true</SpecificVersion>
15 <HintPath>..\libs\PostSharp\PostSharp.Public.dll</HintPath>
16 </Reference>
17 <!-- any other references -->
18 </ItemGroup>



注意:

a. 上述节点中的HintPath,使用的相对路径;引用路径不正确,将出现信息II中的错误。

b. PostSharp.Public的SpecificVersion属性,默认为False,需要在IDE->refrences->PostSharp.Public->Property->SpecificVersion中修改,或在项目文件中手动修改。否则,将出现如信息I中的错误。

3、在项目文件的末端,添加PostSharp的Import信息。此信息指定PostSharp在编译时通过何处的配置文件进行切入。

1   <PropertyGroup>
2 <DontImportPostSharp>True</DontImportPostSharp>
3 </PropertyGroup>
4 <Import Project="..\libs\PostSharp\PostSharp-1.5.targets"/>
5 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>

先找到<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>,分别在节点上面和下面配置如上信息。据文档说,位置很重要(如下)。还特意加了粗体,我测试确认了一下,的确是忽悠的。XML文件的节点,理论上是不应该有顺序的。

Open the project file (csproj, vbproj, ...) using a text editor. At the end of the file, there should be one or more <Import .../> elements. For instance, a C# 2.0 project file ends like this:

 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

Before the import line, insert the following text:

  <PropertyGroup>
    <DontImportPostSharp>True</DontImportPostSharp>
  </PropertyGroup>

This property is important in case that PostSharp is enabled globally on a machine. It prevents PostSharp global targets to be included in the project when C# or VB targets will be included. Since you can never be sure that PostSharp is not installed globally the machine of another developer working on the same project, it is a good practice to define the DontImportPostSharp before importing main C# / VB targets. Note that it is illegal to import PostSharp targets twice in a project; you will therefore get a build error if you accidently try to do so.

After the C# / VB import line, insert the following text:

<Import Project="Path\To\PostSharp\PostSharp.targets" />

4、如果不是人品特别有问题的话,应该可以正确编译了。Over!

文件下载:PostSharp 1.5 XCOPY for x64

posted @ 2011-11-06 01:40  ZhZhen  阅读(1935)  评论(0编辑  收藏  举报