久久久久久久性爱潮,国产又粗又猛又爽又黄下载视频,尤物国产在线一区视频,欧美日本国产在线不卡

<sup id="4uqqy"><ol id="4uqqy"></ol></sup>
  • <th id="4uqqy"></th>

      • <strike id="4uqqy"><nobr id="4uqqy"><ins id="4uqqy"></ins></nobr></strike><sup id="4uqqy"></sup><strong id="4uqqy"><u id="4uqqy"></u></strong>
        <sub id="4uqqy"></sub>

          綠色資源網(wǎng):您身邊最放心的安全下載站! 最新軟件|熱門排行|軟件分類|軟件專題|廠商大全

          綠色資源網(wǎng)

          技術(shù)教程
          您的位置:首頁網(wǎng)絡(luò)編程.Net編程 → .net 2.0 和 4.0不同的.net版本的條件編譯

          .net 2.0 和 4.0不同的.net版本的條件編譯

          我要評(píng)論 2012/11/28 13:15:36 來源:綠色資源網(wǎng) 編輯:www.kiwanis-srv.org [ ] 評(píng)論:0 點(diǎn)擊:348次

          今天碰到這樣一個(gè)需求,寫的C#庫(kù),有時(shí)候需要在.net 2.0下編譯,有時(shí)候需要在.net 4.0下編譯,這個(gè)庫(kù)里使用了lambda表達(dá)式,使用了擴(kuò)展方法,使用了幾個(gè) System.Core.dll 引入的Action類型。為了在 .net 2.0 下能夠編譯成功,我寫了一個(gè)文件 Patch.cs,定義了 System.Runtime.CompilerServices.ExtensionAttribute  類型,這樣就可以在2.0下使用lambda表達(dá)式和擴(kuò)展方法了,同時(shí),添加了幾個(gè)用到的System.Core.dll 引入的Action類型:

          1: namespace System.Runtime.CompilerServices
          2: {
          3: public class ExtensionAttribute : Attribute { }
          4: } 5: 6: namespace System
          7: {
          8: public delegate void Action();
          9: public delegate void Action<T0,T1>(T0 t0,T1 t1);
          10: }
          11:

          然而,要在.net 4.0 下編譯,因?yàn)轭愋鸵呀?jīng)存在,必須注釋掉Patch.cs,很麻煩。于是想通過條件編譯來解決,即:

          1: #if NET2
          2:
          3: namespace System.Runtime.CompilerServices
          4: {
          5: public class ExtensionAttribute : Attribute { }
          6: }
          7:
          8: namespace System
          9: {
          10: public delegate void Action();
          11: public delegate void Action<T0,T1>(T0 t0,T1 t1);
          12: }
          13:
          14: #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 文件修改為:

          1: #region include "Csmacro_Template.cs"
          2: #endregion
          3:
          4: #if NET2
          5:
          6: namespace System.Runtime.CompilerServices
          7: {
          8: public class ExtensionAttribute : Attribute { }
          9: }
          10:
          11: namespace System
          12: {
          13: public delegate void Action();
          14: public delegate void Action<T0,T1>(T0 t0,T1 t1);
          15: }
          16:
          17: #endif

          #region include 是我引入的 Csmacro 宏語法。詳見 C#模板編程(2): 編寫C#預(yù)處理器,讓模板來的再自然一點(diǎn) 一文。點(diǎn)擊編譯,系統(tǒng)會(huì)生成一個(gè) Patch_Csmacro.cs 文件,內(nèi)容如下:

          1: #define NET2
          2:
          3: #if NET2
          4:
          5: namespace System.Runtime.CompilerServices
          6: {
          7: public class ExtensionAttribute : Attribute { }
          8: }
          9:
          10: namespace System
          11: {
          12: public delegate void Action();
          13: public delegate void Action<T0,T1>(T0 t0,T1 t1);
          14: }
          15:
          16: #endif

          第四步,把生成的 Patch_Csmacro.cs 添加到項(xiàng)目中來。

          搞定以后,選擇不同的target,編譯時(shí)產(chǎn)生的就是對(duì)該target的條件編譯!

          關(guān)鍵詞:.net

          閱讀本文后您有什么感想? 已有 人給出評(píng)價(jià)!

          • 0 歡迎喜歡
          • 0 白癡
          • 0 拜托
          • 0 哇
          • 0 加油
          • 0 鄙視