从 MSBuild 3.5 而非 Visual Studio 编译时类型推断失败 (CS0246)

发布于 2024-10-29 03:14:42 字数 927 浏览 4 评论 0 原文

当从命令行编译一些最近的更改时,我们刚刚在构建服务器上遇到了一个有趣的错误:

c:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe

错误 CS0246:找不到类型或命名空间名称“var”(是否缺少 using 指令或程序集引用?)

有问题的代码:

var response = _producerErrors.CreateTextMessage(); 

_ ProducerErrors IMessageProducer的类型 来自 Apache.NMS
responseITextMessage 的类型

请注意,它从 Visual Studio 成功编译,我可以从命令行在本地重现错误,我认为这排除了构建服务器的配置问题。

当然,修复是微不足道的,但它让我困惑为什么编译只能从 MSBuild 命令行失败?该项目还使用了其他 var 关键字,这些关键字编译得很好,所以我怀疑这可能与 Apache 程序集有关,但是什么呢?

We just hit an interesting error on our build server when compiling some recent changes from the command line:

c:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe

error CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

Code in question:

var response = _producerErrors.CreateTextMessage(); 

_producerErrors is type of IMessageProducer from Apache.NMS
response is type of ITextMessage

Note that it successfully compiles from Visual Studio, and I can reproduce the error locally from the command line, which I think rules out a configuration issue with the build server.

The fix is trivial of course but it's got me stumped as to why compilation is only failing from MSBuild command line? The project in question has other var keywords in use which are compiling just fine, so I suspect it might be something to do with the Apache assembly, but what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是你 2024-11-05 03:14:42

您完全确定 3.5 目录中没有偶然出现 2.0 编译器吗?

Are you entirely sure you don't have a 2.0 compiler in your 3.5 directory by accident?

来日方长 2024-11-05 03:14:42

在尝试创建多框架目标 NuGet 包时,我刚刚遇到了类似的问题。将目标框架设置为 3.5 时,代码在 Visual Studio 2010 中编译良好,但未在命令行中编译。经过一些实验,我找到了一个成功的命令行解决方案,尽管我并不完全清楚根本问题是什么。

供参考:

使用 4.0 引用成功编译程序集(通过 DotPeek 检查确认):

C:\BuildAgent\work\efbf25fbf21ee436>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe My.csproj /p:Configuration=Release;TargetFrameworkVersion=v4.0;ToolsVersion=4.0 /t:Rebuild

注意 MSBuild 输出:

Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.296]

使用 2.0/3.5 引用成功编译程序集(通过 DotPeek 检查确认) ):

C:\BuildAgent\work\efbf25fbf21ee436>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe My.csproj /p:Configuration=Release;TargetFrameworkVersion=v3.5;ToolsVersion=3.5 /t:Rebuild

记下 MSBuild 输出:

Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.296]

编译失败

C:\BuildAgent\work\efbf25fbf21ee436>C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe My.csproj /p:Configuration=Release;TargetFrameworkVersion=v3.5;ToolsVersion=3.5 /t:Rebuild

记下 MSBuild 输出:

Microsoft (R) Build Engine Version 3.5.30729.4926
[Microsoft .NET Framework, Version 2.0.50727.4984]

I just ran into a similar problem when attempting to create a multi-framework targeted NuGet package. The code compiled fine in Visual Studio 2010 when setting the target framework to 3.5 but did not compile at the command line. After some experimentation, I found a winning command line solution although it is not entirely clear to me what the underlying problem is.

For reference:

Successfully compiles assembly with 4.0 references (confirmed by inspection via DotPeek):

C:\BuildAgent\work\efbf25fbf21ee436>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe My.csproj /p:Configuration=Release;TargetFrameworkVersion=v4.0;ToolsVersion=4.0 /t:Rebuild

Note the MSBuild output:

Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.296]

Successfully compiles assembly with 2.0/3.5 references (confirmed by inspection via DotPeek):

C:\BuildAgent\work\efbf25fbf21ee436>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe My.csproj /p:Configuration=Release;TargetFrameworkVersion=v3.5;ToolsVersion=3.5 /t:Rebuild

Note the MSBuild output:

Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.296]

Fails to compile:

C:\BuildAgent\work\efbf25fbf21ee436>C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe My.csproj /p:Configuration=Release;TargetFrameworkVersion=v3.5;ToolsVersion=3.5 /t:Rebuild

Note the MSBuild output:

Microsoft (R) Build Engine Version 3.5.30729.4926
[Microsoft .NET Framework, Version 2.0.50727.4984]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文