废话少说,不知道postSharp是什么的可以百度一下。
一、配置
1、下载到的源码打开解决方案时会报错,说是找不到Config.targets。解决办法:从Build文件夹下找到Config.targets.ori,把名字改为Config.targets
2、进入源码文件夹下的Build子文件夹,把PostSharp-32bit.reg的数据导入到注册表,不然会在编译时会提示强名称验证失败。如果你不想用原来的强名称密钥对,在更换的时候要记得修改这个注册表文件里的publickeytoken。
3、下载Nant,有个项目需要Nant
4、添加Nant.Core.dll的引用,编译
5、新建一个文件夹,把所有项目输出目录下的所有文件全部拷贝到该目录下,如C:\PostSharp1.5
6、添加环境变量POSTSHARP15,值为第4步你所建立的文件夹完整路径
7、查看C:\Program Files\MSBuild\v3.5目录,确认是否存在Custom.After.Microsoft.Common.targets文件,如果已经存在,打开该文件,在<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">的下面插入
<Import Project="C:\PostSharp1.5\PostSharp-1.5.targets" Condition=" '$(DontImportPostSharp)' == 'False' AND Exists('C:\PostSharp1.5\PostSharp-1.5.targets')" />;
如果不存在,自己新建一个,输入
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="C:\PostSharp1.5\PostSharp-1.5.targets" Condition=" '$(DontImportPostSharp)' == 'False' AND Exists('C:\PostSharp1.5\PostSharp-1.5.targets')" /> </Project>
注意:C:\PostSharp1.5\PostSharp-1.5.targets这个值是根据第4步实际路径而定
8、在C:\Program Files\MSBuild文件夹下新建PostSharp子文件夹,在PostSharp文件夹下建立PostSharp-1.5.targets文件,输入
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PostSharpUseCommandLine Condition="'$(PostSharpUseCommandLine)'==''">True</PostSharpUseCommandLine>
</PropertyGroup> <Import Project="C:\PostSharp1.5\PostSharp-1.5.targets"/>
</Project>
至此已经全部配置完成。
二、使用
1、新建一个项目
2、在此项目下新建一文件PostSharp.targets,文件内容为
<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Test" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <!-- This file detects the PostSharp installation directory according to deployment properties. It defines the PostSharpTargets property pointing to the target file that should actually be imported after importing the compiler. Every project should define the property DontImportPostSharp=True before importing C#/VB targets. --> <!-- Use a special target file when we are in source tree --> <Import Project="PostSharp-Samples-Src.targets" Condition="''!=''" /> <!-- Other locations --> <PropertyGroup> <!-- This is where the targets file is supposed to be in the distribution tree--> <PostSharpDirectory Condition="'$(PostSharpDirectory)'=='' AND Exists('..\PostSharp-1.5.targets')">..</PostSharpDirectory> <!-- We can also try the MSBuild extension directory --> <PostSharpDirectory Condition="'$(PostSharpDirectory)'=='' AND Exists('$(MSBuildExtensionsPath)\PostSharp\PostSharp-1.5.targets')">$(MSBuildExtensionsPath)\PostSharp</PostSharpDirectory> <!-- Try the environment variable --> <PostSharpDirectory Condition="'$(PostSharpDirectory)'=='' AND Exists('$(POSTSHARP15)\PostSharp-1.5.targets')">$(POSTSHARP15)</PostSharpDirectory> <!-- Error. We did not find it. --> </PropertyGroup> <Import Project="$(PostSharpDirectory)\PostSharp-1.5.targets" Condition="'$(PostSharpDirectory)'!=''" /> <Target Name="BeforeBuild" Condition="'$(PostSharp15Imported)'!='True'"> <Exec Command="cmd /c cd"/> <Error Text="Cannot find PostSharp." /> </Target> </Project>
3、用记事本打开项目文件csproj,在适当位置加入以下内容
<PropertyGroup> <DontImportPostSharp>True</DontImportPostSharp> </PropertyGroup> <Import Project="PostSharp.targets" />
4、添加PostSharp.Core.dll、PostSharp.Laos.dll、PostSharp.Laos.Weaver.dll、PostSharp.Public.dll的引用
PostSharp 1.5并不支持.NET 4.0,VS2010下使用会导致VS崩溃,现在正研究怎么让1.5也支持.NET 4.0。2.0版本支持.NET 4.0,有免费版本和商业版本。
编译好的PostSharp1.5
https://files.cnblogs.com/itgiant/PostSharp1.5.rar
测试代码下载