我在使用 Visual Studio 2019 Preview 测试 C# 9.0 的新功能时一直遇到这个问题。我正在测试 init setter,但编译器显示错误并显示以下消息:
错误 CS0518 未定义或导入预定义类型“System.Runtime.CompilerServices.IsExternalInit”。下面是我尝试过的代码片段:
public class Book
{
string ISBN { get; init; }
}
这是 Visual Studio 2019 中的一个小错误,尚未修复。为了解决这个问题,您需要添加一个名为的虚拟类IsExternalInit
与命名空间System.Runtime.CompilerServices
项目中的任何位置。这样就可以了。
如果编写一个库,最好将此类设为内部类,否则最终可能会得到两个都定义相同类型的库。
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit {}
}
Edit(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 https://developercommunity.visualstudio.com/content/problem/1244809/error-cs0518-predefined-type-systemruntimecompiler.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)