針對(duì)不同.NET版本的條件編譯
為了在 .NET 2.0 下能夠編譯成功,我寫了一個(gè)文件 Patch.cs,定義了 System.Runtime.CompilerServices.ExtensionAttribute 類型,這樣就可以在2.0下使用lambda表達(dá)式和擴(kuò)展方法了,同時(shí),添加了幾個(gè)用到的System.Core.dll 引入的Action類型:
- namespace System.Runtime.CompilerServices
- {
- public class ExtensionAttribute : Attribute { }
- }
- namespace System
- {
- public delegate void Action();
- public delegate void Action<T0,T1>(T0 t0,T1 t1);
- }
然而,要在.NET 4.0 下編譯,因?yàn)轭愋鸵呀?jīng)存在,必須注釋掉Patch.cs,很麻煩。于是想通過條件編譯來解決,即:
- #if NET2
- namespace System.Runtime.CompilerServices
- {
- public class ExtensionAttribute : Attribute { }
- }
- namespace System
- {
- public delegate void Action();
- public delegate void Action<T0,T1>(T0 t0,T1 t1);
- }
- #endif
問題是,.net 里沒有定義和.net版本有關(guān)的指示符。怎么辦呢?自己動(dòng)手,豐衣足食,使用Build Events在編譯之前自動(dòng)偵測(cè)出項(xiàng)目所使用的.net版本,定義出我們想要的指示符。
在 C#模板編程(2): 編寫C#預(yù)處理器,讓模板來的再自然一點(diǎn)一文中,寫了一個(gè)程序 Csmacro.exe 來實(shí)現(xiàn)C#下的模板機(jī)制,本文在Csmacro.exe 的基礎(chǔ)上,增加偵測(cè)項(xiàng)目所引用的.net 版本的功能。
原理:查找項(xiàng)目目錄下的 csproj 文件,解析它,找到節(jié)點(diǎn)TargetFrameworkVersion,判斷.net版本,然后生成一個(gè)Csmacro_Template.cs文件,在里面 #define 版本指示符。例如,對(duì) .Net 2.0 項(xiàng)目,生成的 Csmacro_Template.cs 文件內(nèi)容為:
#define NET2
修改后Csmacro的代碼可在:https://github.com/xiaotie/GebCommon上下載(目前只處理了 .net 2.0 和 4.0,如需要針對(duì)其它版本,可自行修改代碼)。有了 Csmacro,一切就好辦了。
***步,把 Csmacro.exe 放在Path路徑下
第二步,打開需要條件編譯的項(xiàng)目,添加 Pre-build 事件:Csmacro.exe $(ProjectDir)
第三步,編輯源文件,如,Patch.cs 文件修改為:
- #region include "Csmacro_Template.cs"
- #endregion
- #if NET2
- namespace System.Runtime.CompilerServices
- {
- public class ExtensionAttribute : Attribute { }
- }
- namespace System
- {
- public delegate void Action();
- public delegate void Action<T0,T1>(T0 t0,T1 t1);
- }
- #endif
#region include 是我引入的 Csmacro 宏語法。詳見 C#模板編程(2): 編寫C#預(yù)處理器,讓模板來的再自然一點(diǎn) 一文。點(diǎn)擊編譯,系統(tǒng)會(huì)生成一個(gè) Patch_Csmacro.cs 文件,內(nèi)容如下:
- #define NET2
- #if NET2
- namespace System.Runtime.CompilerServices
- {
- public class ExtensionAttribute : Attribute { }
- }
- namespace System
- {
- public delegate void Action();
- public delegate void Action<T0,T1>(T0 t0,T1 t1);
- }
- #endif
第四步,把生成的 Patch_Csmacro.cs 添加到項(xiàng)目中來。
搞定以后,選擇不同的target,編譯時(shí)產(chǎn)生的就是對(duì)該target的條件編譯!
原文鏈接:http://www.cnblogs.com/xiaotie/archive/2012/11/26/2789810.html
【編輯推薦】