Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't write an expression-bodied function member

NOTE: This appears to be a problem with the compiler that's used with SSDT projects, it's apparently fixed in the 2017 RC. My problem is similar to one described here.

I've got some code which refuses to let me write it as an expression-bodied function member. In short, I want to do this:

void foo() => bar();

But the IDE throws a tantrum and demands I write it like this:

void foo() { bar(); }

I mean sure, it's two extra characters but I'm not sure why it's complaining, the errors don't make sense either. It gives me the following 3 errors:

  • CS0000: ; expected
  • CS0000: Method must have a return type.
  • CS0000: Identifier expected.

The full code looks like this.

public static void foo() => bar("some param"); // Errors on this line.
static void bar(string myParam) { //20 lines of code } 

I've tested this in the C# interactive window and everything compiles and runs correctly. I can't find any unprintable characters in the code.

This is using VS 2015 community with the target framework being 4.6.1

Full code:

using System.Data.SqlClient;
using Microsoft.SqlServer.Server;

public partial class Triggers
{

    private const string ConnectionString = "context connection = true";

    private const string ReadInsertedTable = @"
    SELECT ID,
           (
               SELECT *
               FROM inserted AS b
               WHERE a.ID = b.ID
               FOR XML RAW, ELEMENTS XSINIL
           )
    FROM inserted AS a
";
    [SqlTrigger(Name = "Person_Insert", Target = "Person", Event = "FOR INSERT")]
    public static void Person_Insert() => AuditInsert(TableName); //  All errors here.

    private const string TableName = "Person";

    private static void AuditInsert(string tableName)
    {

        using (var readConnection = new SqlConnection(ConnectionString))
        using (var writeConnection = new SqlConnection(ConnectionString))
        {
            using (var readCommand = new SqlCommand(ReadInsertedTable, readConnection))
            {
                readConnection.Open();
                using (var reader = readCommand.ExecuteReader())
                {
                    SqlContext.Pipe.Send((reader));
                }
            }
        }
    }
}

enter image description here

Update: Code compiles using the msbuild utility but still fails in Visual Studio.

like image 516
Jake Avatar asked Feb 04 '17 07:02

Jake


1 Answers

This will have something to do with Roslyn. Try to run this:

Non-roslyn: https://dotnetfiddle.net/LJm1Fj

Roslyn: https://dotnetfiddle.net/aMUsj0

I suspect there's something wrong either with your project file or your Visual Studio installation because VS2015 should use Roslyn-based compiler by default.

I'd try:

  • creating a new project with the code from the fiddle above
  • if it compiles then compare the differences in the *.csproj files, mainly the ToolsVersion <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> and targets <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • you should also look in the Output->Build window in VS and check out the executable being launched, in my case: C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe /noconfig /nowarn:1701,1702,2008 /nostdlib+ /platform:anycpu32bitpreferred /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation /preferreduilang:en-US /highentropyva+ /reference:"C:\Program Files (x86)\Reference ... Using shared compilation with compiler from directory: C:\Program Files (x86)\MSBuild\14.0\bin
  • if it doesn't compile, I'd suggest reinstalling .NET and VS
like image 140
rocky Avatar answered Oct 03 '22 07:10

rocky