Automatically implemented properties must define both get and set accessors

 ̄綄美尐妖づ 提交于 2019-12-12 20:23:44

问题


SQLCLR Visual Studio 2015

I'm new to writing CLR code.

I'm getting the following error when compiling a SQL CLR Function

I'm using the .Net Coordinates library.

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.

My understanding is that by using Visual Studio I'm using C# v6.

Could the .sqlproj be forcing the use of a earlier version of C#?


回答1:


The main issue seems to be a disconnect between Visual Studio and the .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.

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:

Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default

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. But, if you go to a command prompt, go to the folder containing Csc.exe and run it directly (i.e. just Csc at the prompt), you should see the follow note:

Microsoft (R) Visual C# Compiler version 4.6.1590.0
for C# 5
Copyright (C) Microsoft Corporation. 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. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

That link takes you to the GitHub repository for Roslyn. 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):

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




回答2:


If you don't want to use C# 6, implement the getter with a private backing field:

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

Set the value of _datum somewhere inside the class (ctor, init method, ...).



来源:https://stackoverflow.com/questions/46447389/automatically-implemented-properties-must-define-both-get-and-set-accessors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!