未定义或导入预定义类型'System.Runtime.CompilerServices.IsExternalInit'

凯文

在使用Visual Studio 2019 Preview测试C#9的新功能时遇到了这个问题。我正在测试初始化​​设置器,但是编译器显示以下错误消息:

错误CS0518未定义或导入了“ System.Runtime.CompilerServices.IsExternalInit”预定义类型。以下是我尝试过的代码片段:

public class Book
{
     string ISBN { get; init; }
}
奎宁·罗扎(Quinine Roza)

这是Visual Studio 2019中的一个小错误,尚未修复。为了解决这个问题,您需要在项目的任何地方添加一个以IsExternalInit名称空间命名的伪类System.Runtime.CompilerServices这样就可以了。

如果编写一个库,最好将这个类设置为内部类,否则您可能会得到两个都定义相同类型的库。

    namespace System.Runtime.CompilerServices
    {
          internal static class IsExternalInit {}
    }

编辑(2020年11月16日):

根据我从C#语言团队的原则开发负责人Jared Parsons得到的答复来看,以上问题不是错误。编译器会引发此错误,因为我们正在针对较旧的.NET Framework版本编译.NET 5代码。请参阅下面的消息:

感谢您抽出宝贵时间提出此反馈问题。不幸的是,这不是错误。IsExternalInit类型仅包含在net5.0(以及将来的)目标框架中。在针对较旧的目标框架进行编译时,您将需要手动定义此类型。

链接到Visual Studio开发人员社区上的报告:https : //developercommunity.visualstudio.com/content/problem/1244809/error-cs0518-predefined-type-systemruntimecompiler.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在VS2019中测试C#9.0-CS0518未定义或导入IsExternalInit ...如何定义/导入它?

未定义或导入预定义类型“ System.ValueTuple´2”

未定义预定义类型'System.Object'或未导入.net 4.6

无法从程序集“ mscorlib”加载类型“ System.Runtime.CompilerServices.ExtensionAttribute”

未捕获的ReferenceError:在sp.runtime.js中未定义类型

缺少编译器必需的成员'System.Runtime.CompilerServices.ExtensionAttribute..ctor'

完全无法定义System.Runtime.Caching的UpdateCallback

chrome.runtime.onInstalled未定义

未定义或导入预定义类型System.Object

在ASP.NET 5的Visual Studio Online构建期间未定义或导入预定义类型'System.Void'

在类型为coldfusion.runtime.J2eeSessionScope的Java对象中未定义元素

System.Runtime.CompilerServices.AsyncTaskMethodBuilder :: Start的自拍时间很长

升级到.NET 4.7后,“未定义或导入预定义类型System.ValueTuple”

“未定义或导入预定义类型'System。*'”错误,Visual Studio代码,omnisharp

未定义或导入奇怪的诊断错误,首选类型为System ...

System.Runtime.CompilerServices.Unsafe在Xamarin表单中产生意外行为

C#预定义类型'System.Object'未定义或导入

未定义或导入预定义类型System.Range

“无法解决T [] modreq(System.Runtime.CompilerServices.IsVolatile)”

无法加载文件或程序集System.Runtime.CompilerServices.Unsafe

应用程式无法在docker中运作,无法载入档案或程序集'System.Runtime.CompilerServices.Unsafe

System.Runtime.CompilerServices.AsyncServices。<ThrowAsync> b__1()中的NullReferenceException

没有我的代码在堆栈跟踪中的System.Runtime.CompilerServices.AsyncServices中的异常

C#-Unity内部编译器错误:“ System.Runtime.CompilerServices.CallSite” CS0518-使用动态变量时

在程序集中定义了“ System.Runtime.Serialization.IExtensibleDataObject”

无法从程序集中加载类型'System.Runtime.CompilerServices.ExtensionAttribute',但不是mscorlib

NET标准库中的System.Runtime.CompilerServices

System.Runtime.CompilerServices.Unsafe 在 Xamarin 中运行 .Net Standard 项目

对类型“MarshalByRefObject”的引用声称它在“System.Runtime”中定义,但找不到