无法编写表达式函数成员

Can't write an expression-bodied function member

注意: 这似乎是与 SSDT 项目一起使用的编译器的问题,它显然已在 2017 RC 中修复。我的问题与描述的问题类似

我有一些代码拒绝让我把它写成一个表达式函数成员。简而言之,我想这样做:

void foo() => bar();

但是 IDE 发脾气,要求我这样写:

void foo() { bar(); }

我的意思是肯定,这是两个额外的字符,但我不确定它为什么会抱怨,这些错误也没有意义。它给了我以下 3 个错误:

完整代码如下所示。

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

我已经在 C# 交互式 window 中对此进行了测试,一切都可以正确编译和运行。我在代码中找不到任何不可打印的字符。

这是使用 VS 2015 社区,目标框架是 4.6.1

完整代码:

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));
                }
            }
        }
    }
}

更新: 代码使用 msbuild 实用程序编译,但在 Visual Studio.

中仍然失败

这将与罗斯林有关。尝试 运行 这个:

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

罗斯林: https://dotnetfiddle.net/aMUsj0

我怀疑您的项目文件或您的 Visual Studio 安装有问题,因为 VS2015 应该默认使用 Roslyn-based 编译器。

我会尝试:

  • 使用上面 fiddle 中的代码创建一个新项目
  • 如果它编译然后比较 *.csproj 文件中的差异,主要是 ToolsVersion <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 和目标 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • 你还应该在 VS 中查看 Output->Build window 并检查正在启动的可执行文件,在我的例子中:C:\Program Files (x86)\MSBuild.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.0\bin
  • 如果无法编译,我建议重新安装 .NET 和 VS

这个答案在很大程度上是无关紧要的。

正如提问者自己发现的那样,在用户 rocky 的帮助下,问题是 SQL 服务器数据库项目 (.sqlproj)(不像.csproj) 类型的其他项目不是使用新的 C# 6.0 编译器构建的,至少在 Visual Studio 2015 中不是。提问者 链接的线程有一些细节。


旧答案:

在你的图像中,常量声明:

private const string TableName = "Person";

不见了。但是,您还没有从 AuditInsert 方法中删除 tableName 参数。