3

I have a C# library in Visual Studio 2017, and I'm trying to use a tuple in an interface:

IEnumerable<(Guid Id, string name)> GetFoo ();

I have added a reference (via NuGet) to System.ValueTuple.

Visual Studio and ReSharper both do not detect problems with this line, but when I build I get errors:

------ Build started ------ 
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Workflow.targets(121,5):
error : Type expected
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Workflow.targets(121,5):
error : Invalid token '(' in class, struct, or interface member
declaration
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Workflow.targets(121,5):
error : Identifier expected; 'string' is a keyword
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Workflow.targets(121,5):
error : ; expected
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Workflow.targets(121,5):
error : Method must have a return type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This suggests that it is not being compiled using C# 7.

I am targetting .NET Framework 4.6.2.

Things I've tried:

  • Installing the latest updates to Visual Studio
  • Making the project compile using C# 7 explicitly, via Properties > Build > Advanced > Language Version
  • Adding a reference to Microsoft.Net.Compilers

None has had an effect.


Edit: Not a duplicate of C# 7 .NET / CLR / Visual Studio version requirements, as solution there (NuGet package) did not solve issue.

Paul
  • 16,285
  • 13
  • 41
  • 52
  • 2
    Possible duplicate of [C# 7 .NET / CLR / Visual Studio version requirements](https://stackoverflow.com/questions/42672957/c-sharp-7-net-clr-visual-studio-version-requirements) – labilbe Feb 15 '18 at 15:36
  • 2
    That's odd. "*Making the project compile using C# 7 explicity, via Properties > Build > Advanced > Language Version*" should have been the solution to this issue. – David Arno Feb 15 '18 at 15:36
  • Looks like some sort of pre-compilation is not happening... but just a wild guess from me. – theMayer Feb 15 '18 at 15:40
  • Tuples are working. Thousands of developers use them. The issue is why they don't work in your case. BTW you don't need to add anything extra if you target 4.7.1 – Panagiotis Kanavos Feb 15 '18 at 15:41
  • If I use `interface moo{ IEnumerable<(Guid Id, string name)> GetFoo (); }` in LinqPad 5 I get no errors. Have you tried creating a new empty Console project containing only an interface declaration? – Panagiotis Kanavos Feb 15 '18 at 15:42
  • 3
    Are you targeting `C# latest major version`, `C# latest minor version` or `C# 7.0`? – David Arno Feb 15 '18 at 15:42
  • 2
    Thanks all. I don't think it's a duplicate. I'm not able to target 4.7.1 due to wider environment constraints. – Paul Feb 15 '18 at 15:56
  • The same code *does* work in a new project. Nothing about the library it's not working in seems odd. Any ideas what to look for rather than recreating the project from scratch? – Paul Feb 15 '18 at 15:56
  • @DavidArno: I get the same problem with all three – Paul Feb 15 '18 at 15:57
  • Can you share the csproj file, or is it proprietary? – David Arno Feb 15 '18 at 15:58
  • Unfortunately it's proprietary, but I think I've found the bit that's the problem (see below) – Paul Feb 15 '18 at 16:59
  • I don't know if it helps, but those errors are not coming from a C# 7.0 compiler, but an older compiler. – Julien Couvreur Feb 15 '18 at 17:10

1 Answers1

2

I think I've worked out the source of the problem.

The project imports:

<Import Project="$(MSBuildToolsPath)\Workflow.Targets" />

Which is the source of the errors in the build output. If I import this into a trivial console project with the above code, I can reproduce the error.

Paul
  • 16,285
  • 13
  • 41
  • 52
  • I don't think so. From the build log, it's using `C:\Windows\Microsoft.NET\Framework\v4.0.30319\Workflow.targets`, which would is the right path for my version of .Net. I'm currently trying to work out whether I can just remove this include (finding out what `Workflow.Targets` is doing seems somewhat hard!) – Paul Feb 15 '18 at 17:20
  • You can remove it but I wonder how that ended up in your console project. If you open it, it's the `targets` file for Windows Workflow. It shouldn't appear inside a console project - it's *not* part of the template. Did you add any libraries or packages that reference WF? – Panagiotis Kanavos Feb 15 '18 at 17:26
  • It's a library rather than a console project. It's part of a very old code-base, so it may be the case that it was needed at some point in the past but not now. I can't think of a reason that it'd be needed. I'll remove it and put it through the continuous integration/regression tests to see whether it's broken anything. Thank you for your help! – Paul Feb 15 '18 at 17:31
  • if you create a new console application or library you *shouldn't* see any references to `Workflow.targets` in the `.csproj` file. That's what makes this so strange. – Panagiotis Kanavos Feb 16 '18 at 08:31