当从命令行编译一些最近的更改时,我们刚刚在构建服务器上遇到了一个有趣的错误:
c:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe
错误 CS0246:找不到类型或命名空间名称“var”(是否缺少 using 指令或程序集引用?)
有问题的代码:
var response = _producerErrors.CreateTextMessage();
_ ProducerErrors 是IMessageProducer的类型 来自 Apache.NMS
response
是 ITextMessage 的类型
请注意,它从 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?
发布评论
评论(2)
您完全确定 3.5 目录中没有偶然出现 2.0 编译器吗?
Are you entirely sure you don't have a 2.0 compiler in your 3.5 directory by accident?
在尝试创建多框架目标 NuGet 包时,我刚刚遇到了类似的问题。将目标框架设置为 3.5 时,代码在 Visual Studio 2010 中编译良好,但未在命令行中编译。经过一些实验,我找到了一个成功的命令行解决方案,尽管我并不完全清楚根本问题是什么。
供参考:
使用 4.0 引用成功编译程序集(通过 DotPeek 检查确认):
注意 MSBuild 输出:
使用 2.0/3.5 引用成功编译程序集(通过 DotPeek 检查确认) ):
记下 MSBuild 输出:
编译失败:
记下 MSBuild 输出:
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):
Note the MSBuild output:
Successfully compiles assembly with 2.0/3.5 references (confirmed by inspection via DotPeek):
Note the MSBuild output:
Fails to compile:
Note the MSBuild output: