2015-12-16 72 views
10

檢測C#版本我有一箇舊線的C#代碼,基本上是這樣的:在編譯時

foo.set_Parent(parent); 

它已經多年編譯罰款。現在,在VS2015我得到的錯誤:

CS0571 'Foo.Parent.set': cannot explicitly call operator or accessor

所以我可以重寫路線爲:

foo.Parent=parent; 

這建立在VS2015罰款,但在VS2013它給人的錯誤:

'Foo.Parent' is not supported by the language; try directly calling accessor methods 'Foo.get_Parent()' or Foo.set_Parent(Foo)'

所以簡單的解決方法是簡單地根據編譯器運行的版本確定這兩行。但是,如何檢測編譯器的哪個版本正在執行?

並且爲了記錄,不,我不能僅僅指示團隊中的每個人都同時升級到VS2015。

其他信息 - 給大家聞老鼠,我會繼續和拖出醜陋的真相,但我不認爲這會改變任何東西。 Foo班來自一個古老的Borland大會,這個大會在德爾福都有聯繫(是的,我們正在遷移,但還沒有到那裏)。所以實際的代碼,可以編譯高達VS2013,看起來是這樣的:

using Borland.Vcl; 
using RepGen; 
using SnapReportsForm; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.InteropServices; 

namespace MigrantCOM { 
    [ComVisible(true)] 
    [Guid("48245BA3-736B-4F98-BDC5-AD86F77E39F4")] 
    [ProgId("MigrantCOM.Exports")] 
    [ClassInterface(ClassInterfaceType.AutoDual)] 
      public class MigrantCLRExports { // : MarshalByRefObject 
     public string Test(string s) { return s+s; } 
    } 

    [ComVisible(true)] 
    [Guid("1154D364-B588-4C31-88B9-141072303117")] 
    [ProgId("MigrantCOM.SnapRepCOM")] 
    [ClassInterface(ClassInterfaceType.AutoDual)] 
    public class SnapRepCOM { 
     TRepGen repGen; 
     TStringList snapRefs=new TStringList(); 
     TForm parent=new TForm(null); 
     TMemo designerMemo; 
     List<TReference> references=new List<TReference>(); 
     TRunAsSnapContext runAsSnapContext=new TRunAsSnapContext(); 

     public SnapRepCOM() { 
     designerMemo=new TMemo(parent); designerMemo.set_Parent(parent); 
     ... 
     } 

因此類被實例化是Borland.Vcl.TMemo這是老德爾福組件的一部分。

+8

對我來說這看起來*很奇怪。就我所知,您無法直接調用訪問者。你能提供一個簡短但完整的例子來證明這個問題嗎? –

+0

如果一切都失敗了,我想反射可以用作解決方法。 –

+0

foo.Parent =父應該在任何版本的c#/ .net中工作...你可以發佈一個foo.Parent是「更多代碼」的例子。 –

回答

1

我要離開這個作爲答案,鏈接圖像比在評論中更適合。

所以,如果你想使用VS 2015年,但仍然使用工作多年的C#語言的相同的良好醇」的版本,你可以配置你的項目爲目標的特定版本:

enter image description here

這在csproj中增加了<LangVersion>5</LangVersion>

+0

這似乎沒有效果,這讓我覺得Hans Passant有正確的想法。 –