开发者

Type inference failing (CS0246) compiling from MSBuild 3.5 but not Visual Studio

开发者 https://www.devze.com 2023-02-20 12:27 出处:网络
We just hit an interesting error on our build server when compiling some recent changes from the command line:

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 Vi开发者_Python百科sual 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?


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


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]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号