rhughes
rhughes

Reputation: 9583

Compile Error When Using `record` Types With Unity3d

I'm trying to use a record with Unity3d. The documentation says C# 9 is now supported, but I still get the error:

The predefined type 'System.Runtime.CompilerServices.IsExternalInit' must be defined or imported in order to declare init-only setter.

The documentation states this, but I'm not sure what it means:

The type System.Runtime.CompilerServices.IsExternalInit is required for full record support as it uses init only setters, but is only available in .NET 5 and later (which Unity doesn’t support). Users can work around this issue by declaring the System.Runtime.CompilerServices.IsExternalInit type in their own projects.

Upvotes: 8

Views: 2825

Answers (1)

rhughes
rhughes

Reputation: 9583

The solution is mentioned in this answer. You need to manually define IsExternalInit somewhere in your project, like this:

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

Upvotes: 9

Related Questions