简体   繁体   English

自动实现的属性必须同时定义 get 和 set 访问器

[英]Automatically implemented properties must define both get and set accessors

SQLCLR Visual Studio 2015 SQLCLR Visual Studio 2015

I'm new to writing CLR code.我是编写 CLR 代码的新手。

I'm getting the following error when compiling a SQL CLR Function编译 SQL CLR 函数时出现以下错误

编译器错误

I'm using the .Net Coordinates library .我正在使用.Net Coordinates 库

The code in question is有问题的代码是

public Datum.Datum Datum { get; }

The same library when compiled in a C# console application (not CLR) builds and executes successfully using Visual Studio 2015.在 C# 控制台应用程序(不是 CLR)中编译时,相同的库使用 Visual Studio 2015 成功构建和执行。

My understanding is that by using Visual Studio I'm using C# v6.我的理解是,通过使用 Visual Studio,我使用的是 C# v6。

Could the .sqlproj be forcing the use of a earlier version of C#? .sqlproj 是否会强制使用早期版本的 C#?

The main issue seems to be a disconnect between Visual Studio and the .NET Framework / CLR.主要问题似乎是 Visual Studio 和 .NET Framework/CLR 之间的脱节。 Visual Studio, it seems, can be updated to use newer versions of C#, independently of any new version(s) actually existing on your machine. Visual Studio 似乎可以更新为使用较新版本的 C#,独立于您的计算机上实际存在的任何新版本。

You shouldn't need to specify using C# 6.0 in the advanced SQLCLR build properties, but doing so gives you an error message that is insightful:不应该需要指定高级SQLCLR生成属性使用C#6.0,但这样做给你一个错误消息见地:

Invalid option '6' for /langversion; /langversion 的选项“6”无效; must be ISO-1, ISO-2, 3, 4, 5 or Default必须是 ISO-1、ISO-2、3、4、5 或默认

Looking at the output messages (assuming a high-enough level) it should show that you are using C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Csc.exe , so it doesn't appear that the .sqlproj file or SSDT is forcing anything here.查看输出消息(假设足够高的级别)它应该表明您正在使用C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Csc.exe ,所以它似乎没有.sqlproj文件或 SSDT 在这里强制执行任何操作。 But, if you go to a command prompt, go to the folder containing Csc.exe and run it directly (ie just Csc at the prompt), you should see the follow note:但是,如果您转到命令提示符,转到包含Csc.exe的文件夹并直接运行它(即在提示符下只运行Csc ),您应该看到以下注释:

Microsoft (R) Visual C# Compiler version 4.6.1590.0 Microsoft (R) Visual C# 编译器版本 4.6.1590.0
for C# 5对于 C# 5
Copyright (C) Microsoft Corporation.版权所有 (C) 微软公司。 All rights reserved.版权所有。

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version.此编译器作为 Microsoft (R) .NET Framework 的一部分提供,但仅支持最高 C# 5 的语言版本,该版本不再是最新版本。 For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240有关支持更新版本的 C# 编程语言的编译器,请参阅http://go.microsoft.com/fwlink/?LinkID=533240

That link takes you to the GitHub repository for Roslyn.该链接会将您带到 Roslyn 的 GitHub 存储库。 To make things easier, here is the link directly to that page, starting at the section for downloading the compiler without also downloading Visual Studio (since you already have that):为方便起见,这里是该页面的直接链接,从下载编译器部分开始,无需下载 Visual Studio(因为您已经拥有了):

https://github.com/dotnet/roslyn#download-c-and-visual-basichttps://github.com/dotnet/roslyn#download-c-and-visual-basic

If you don't want to use C# 6, implement the getter with a private backing field:如果您不想使用 C# 6,请使用私有支持字段实现 getter:

public Datum.Datum Datum { get {return _datum; } }
private Datum.Datum _datum;

Set the value of _datum somewhere inside the class (ctor, init method, ...).在类中的某处设置_datum的值( _datum 、初始化方法等)。

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

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