Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Emik03/Emik.Net20Records

Repository files navigation

Emik.Net20Records

NuGet License

Adds C# 9/10's record/record struct support for .NET Framework 2, 3, and 3.5.


Deprecated

As of January 10th 2023, this library will no longer be maintained and is considered obsolete in favor of a better alternative:

  • Add InlineIL.Fody as a dependency, setup of Fody is required.

  • Download this file; a modified version of the .NET Framework 3.5 binary.

  • Reference this binary in your .csproj, or Directory.Build.targets:

<ItemGroup Condition="'$(TargetFramework)' == 'net35'">
  <Reference Include="mscorlib" Aliases="global, ms" Private="false" HintPath="$(SharedPath)Emik.Morsels/References/mscorlib.dll" />
</ItemGroup>

Refer to the prerequisite, then compile.

For more information on how or why this works, click here.


Prerequisite

This library intentionally leaves out System.Runtime.CompilerServices.IsExternalInit to be able to be better integrated with other excellent polyfilling libraries such as Theraot.Core. Either use a package like Manual Roemer's IsExternalInit, or define your own within the project:

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)]
public static class IsExternalInit
{
}

Description

Record types, introduced in C# 9 and 10 are a fantastic feature that allows for easily creating data types with lots of overriden methods that lift a lot of heavy boilerplate out of classes. The feature was originally targetted for .NET 5, but can be used in previous frameworks by polyfilling System.Runtime.CompilerServices.IsExternalInit.

However, prior to .NET Framework 4, the System.Type.op_Equality and System.Type.op_Inequality methods didn't exist. Roslyn still expects them to exist, and will therefore throw an error that this method is missing.

This library adds record and record struct functionality for old .NET Frameworks by create a wrapper class Type that inherits the original, and polyfilling the required op_Equality and op_Inequality static methods by calling System.Type.Equals(Type) which Roslyn scans for.


Contribute

As with most GitHub repositories, you can contribute by: