Following instructions to upgrade my solution to support the new C# language features, I changed my TeamCity build step's MsBuild Version
option from Microsoft .NET 4.5
to Microsoft Build Tools 2015
. I can build the solution fine in Visual Studio 2015, but my Teamcity build fails as follows:
Class.cs(323, 72): error CS0407: 'void Class.Foo()' has the wrong return type
Line 323 of Class.cs looks like this:
LogHelper.WithErrorLogging(this.log, "Foo", false, this.Foo, true);
Class.Foo() has the following signature:
private void Foo()
LogHelper.WithErrorLogging() has several overloads, but the one I want to call is:
public static void WithErrorLogging(ILogger log, string name, bool shouldThrow, Action action, bool logOnlyOnError = false)
As I say, the build succeeds locally in Visual Studio 2015, failing on TeamCity, but if I remove this overload of LogHelper.WithErrorLogging() then the TeamCity build works again:
public static T WithErrorLogging<T>(ILogger log, string name, bool shouldThrow, Func<T> action, T errorValue = default(T), bool logOnlyOnError = false)
My conclusion is that the Microsoft Build Tools 2015 compiler is incorrectly resolving the overload to call, by taking the final true
parameter as the errorValue
parameter of the second overload, thus inferring the generic type argument T
is bool
and finally failing to compile because this.Foo()
is an Action
and not a Func<T>
, but without ever considering the first overload as a possible valid method to call.
Can anyone shed any light on why this should be? Is this a compiler bug, or is there a valid reason why this code ought not to compile as C#6?