gpt4 book ai didi

c# - 在编译时检测 c# 版本

转载 作者:可可西里 更新时间:2023-11-01 09:15:24 25 4
gpt4 key购买 nike

我有一行旧的 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 程序集,它全部绑定(bind)在 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# 语言的同一个好版本,您可以配置您的项目以针对特定版本:

enter image description here

这增加了 <LangVersion>5</LangVersion>在 csproj 中。

关于c# - 在编译时检测 c# 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com