Use C# 9 records in non-net5.0 projects
The new C# 9 records syntax is quite nice:
[DebuggerDisplay("{Name} = {Value}")]
record ResourceValue(string Name, string Value, bool HasFormat)
{
public bool IsIndexed { get; init; }
public List<string> Format { get; } = new List<string>();
}
Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported
To workaround the issue, simply declare the missing type in your project:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
sealed class IsExternalInit
{
}
}
If you're multitargeting net5.0 and other TFMs, just add this bit of MSBuild to remove it for net5.0 since it's built-in:
<ItemGroup>
<Compile Remove="IsExternalInit.cs" Condition="'$(TargetFramework)' == 'net5.0'" />
</ItemGroup>