简体   繁体   English

无法编写表达式功能成员

[英]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. 注意:这似乎是与SSDT项目一起使用的编译器的一个问题,它显然已在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: 但IDE发脾气并要求我这样写:

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: 它给了我以下3个错误:

  • CS0000: ; CS0000:; expected 预期
  • CS0000: Method must have a return type. CS0000:方法必须具有返回类型。
  • CS0000: Identifier expected. CS0000:预期的标识符。

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. 我在C#交互式窗口中对此进行了测试,所有内容都编译并正确运行。 I can't find any unprintable characters in the code. 我在代码中找不到任何不可打印的字符。

This is using VS 2015 community with the target framework being 4.6.1 这是使用VS 2015社区,目标框架是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));
                }
            }
        }
    }
}

在此输入图像描述

Update: Code compiles using the msbuild utility but still fails in Visual Studio. 更新:代码使用msbuild实用程序进行编译,但在Visual Studio中仍然失败。

This will have something to do with Roslyn. 这将与Roslyn有关。 Try to run this: 试着运行这个:

Non-roslyn: https://dotnetfiddle.net/LJm1Fj 非罗斯林: https ://dotnetfiddle.net/LJm1Fj

Roslyn: https://dotnetfiddle.net/aMUsj0 罗斯林: 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. 我怀疑你的项目文件或Visual Studio安装有问题,因为默认情况下VS2015应该使用基于Roslyn的编译器。

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" /> 如果它编译然后比较* .csproj文件中的差异,主要是ToolsVersion <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">和目标<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 您还应该查看VS中的Output-> Build窗口并查看正在启动的可执行文件,在我的例子中: 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 如果它不编译,我建议重新安装.NET和VS.

This answer turned out to be largely irrelevant. 这个答案在很大程度上是无关紧要的。

As the asker figured out himself, with help from user rocky, the issue is that an SQL Server Database Project ( .sqlproj ) (unlike other projects of type .csproj ) is not built with the new C# 6.0 compiler, at least not in Visual Studio 2015. The thread linked by the asker, Usage of wrong compiler during SQL Server Database Project building , has some details. 当提问者在用户问题的帮助下弄清楚自己时,问题是SQL Server数据库项目.sqlproj )(与.csproj类型的其他项目不同)不是使用新的C#6.0编译器构建的,至少在Visual C ++编译器中没有。 Studio 2015.由提问者链接的线程, 在SQL Server数据库项目构建期间使用错误的编译器 ,有一些细节。


Old answer: 老答案:

In your image , the constant declaration: 在你的图像中 ,常量声明:

private const string TableName = "Person";

is missing. 不见了。 However, you have not removed the tableName parameter from the AuditInsert method. 但是,您尚未从AuditInsert方法中删除tableName参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM