在 Unity3d 中使用“record”类型时出现编译错误

rhu*_*hes 5 c# unity-game-engine

我正在尝试使用recordUnity3d。文档说现在支持 C# 9,但我仍然收到错误:

\n
\n

必须定义或导入预定义类型“System.Runtime.CompilerServices.IsExternalInit”才能声明仅限 init 的 setter。

\n
\n

文档说明了这一点,但我不确定这意味着什么:

\n
\n

System.Runtime.CompilerServices.IsExternalInit 类型是完整记录支持所必需的,因为它仅使用 init setter,但仅在 .NET 5 及更高版本中可用(Unity 不\xe2\x80\x99t 支持)。用户可以通过在自己的项目中声明 System.Runtime.CompilerServices.IsExternalInit 类型来解决此问题。

\n
\n

rhu*_*hes 4

这个答案中提到了解决方案。您需要IsExternalInit在项目中的某个位置手动定义,如下所示:

using System.ComponentModel;
namespace System.Runtime.CompilerServices
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    internal class IsExternalInit{}
}
Run Code Online (Sandbox Code Playgroud)