加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在编译时检测c#版本

发布时间:2020-12-15 06:38:58 所属栏目:百科 来源:网络整理
导读:我有一行旧的c#代码看起来基本上是这样的: foo.set_Parent(parent); 它已经编成好几年了.现在在VS2015我得到错误: CS0571 ‘Foo.Parent.set’: cannot explicitly call operator or accessor 所以我可以重写行: foo.Parent=parent; 这在VS2015中构建良好,
我有一行旧的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)’

所以简单的修复就是简单的ifdef这两行基于哪个版本的编译器正在运行.但是如何检测编译器正在执行的版本?

而对于记录,不,我不能只是指示,团队中的每个人同时升级到VS2015.

附加信息 –
对于嗅到老鼠的人,我会继续拖出丑陋的事实,虽然我不认为它会改变很多东西. Foo类来自一个古老的Borland大会,它们都与Delphi结合在一起(是的,我们正在迁移,但还没有迁移).所以编译到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,它是旧的Delphi程序集的一部分.

解决方法

我将此作为答案,连接图像将比在评论中更适合.

所以如果你想使用VS 2015但仍然使用相同的好的版本的C#语言多年,您可以配置您的项目目标一个特定的版本:

这增加了< LangVersion> 5< / LangVersion>在csproj.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读