SimonCropp / Polyfill

Source only package that exposes newer .net and C# features to older runtimes.
MIT License
275 stars 19 forks source link

Polyfill

Build status Polyfill NuGet Status

Source only package that exposes newer .NET and C# features to older runtimes.

The package targets netstandard2.0 and is designed to support the following runtimes.

API count: 367

See Milestones for release notes.

TargetFrameworks

Some polyfills are implemented in a way that will not have the equivalent performance to the actual implementations.

For example the polyfill for StringBuilder.Append(ReadOnlySpan<char>) on netcore2 is:

public StringBuilder Append(ReadOnlySpan<char> value)
    => target.Append(value.ToString());

Which will result in a string allocation.

As Polyfill is implemented as a source only nuget, the implementation for each polyfill is compiled into the IL of the resulting assembly. As a side-effect that implementation will continue to be used even if that assembly is executed in a runtime that has a more efficient implementation available.

As a result, in the context of a project producing nuget package, that project should target all frameworks from the lowest TargetFramework up to and including the current framework. This way the most performant implementation will be used for each runtime. Take the following examples:

Nuget

https://nuget.org/packages/Polyfill/

SDK / LangVersion

This project uses features from the current stable SDK and C# language. As such consuming projects should target those:

LangVersion

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <LangVersion>latest</LangVersion>

global.json

{
  "sdk": {
    "version": "8.0.301",
    "rollForward": "latestFeature"
  }
}

Consuming and type visibility

The default type visibility for all polyfills is internal. This means it can be consumed in multiple projects and types will not conflict.

Consuming in an app

If Polyfill is being consumed in a solution that produce an app, then it is recommended to use the Polyfill nuget only in the root "app project" and enable PolyPublic.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <PolyPublic>true</PolyPublic>

Then all consuming projects, like tests, will not need to use the Polyfill nuget.

Consuming in a library

If Polyfill is being consumed in a solution that produce a library (and usually a nuget), then the Polyfill nuget can be added to all projects.

If, however, InternalsVisibleTo is being used to expose APIs (for example to test projects), then the Polyfill nuget should be added only to the root library project.

Included polyfills

ModuleInitializerAttribute

Reference: Module Initializers

static bool InitCalled;

[Test]
public void ModuleInitTest() =>
    Assert.True(InitCalled);

[ModuleInitializer]
public static void ModuleInit() =>
    InitCalled = true;

snippet source | anchor

IsExternalInit

Reference: init (C# Reference)

class InitSample
{
    public int Member { get; init; }
}

snippet source | anchor

Nullable attributes

Reference: Nullable reference types

Required attributes

Reference: C# required modifier

public class Person
{
    public Person()
    {
    }

    [SetsRequiredMembers]
    public Person(string name) =>
        Name = name;

    public required string Name { get; init; }
}

snippet source | anchor

CompilerFeatureRequiredAttribute

Indicates that compiler support for a particular feature is required for the location where this attribute is applied.

CollectionBuilderAttribute

Can be used to make types compatible with collection expressions

ConstantExpectedAttribute

Indicates that the specified method parameter expects a constant.

SkipLocalsInit

Reference: SkipLocalsInitAttribute

the SkipLocalsInit attribute prevents the compiler from setting the .locals init flag when emitting to metadata. The SkipLocalsInit attribute is a single-use attribute and can be applied to a method, a property, a class, a struct, an interface, or a module, but not to an assembly. SkipLocalsInit is an alias for SkipLocalsInitAttribute.

class SkipLocalsInitSample
{
    [SkipLocalsInit]
    static void ReadUninitializedMemory()
    {
        Span<int> numbers = stackalloc int[120];
        for (var i = 0; i < 120; i++)
        {
            Console.WriteLine(numbers[i]);
        }
    }
}

snippet source | anchor

Index and Range

Reference: Indices and ranges

If consuming in a project that targets net461 or net462, a reference to System.ValueTuple is required. See References: System.ValueTuple.

[TestFixture]
class IndexRangeSample
{
    [Test]
    public void Range()
    {
        var substring = "value"[2..];
        Assert.AreEqual("lue", substring);
    }

    [Test]
    public void Index()
    {
        var ch = "value"[^2];
        Assert.AreEqual('u', ch);
    }

    [Test]
    public void ArrayIndex()
    {
        var array = new[]
        {
            "value1",
            "value2"
        };

        var value = array[^2];

        Assert.AreEqual("value1", value);
    }
}

snippet source | anchor

OverloadResolutionPriority

C# introduces a new attribute, System.Runtime.CompilerServices.OverloadResolutionPriority, that can be used by API authors to adjust the relative priority of overloads within a single type as a means of steering API consumers to use specific APIs, even if those APIs would normally be considered ambiguous or otherwise not be chosen by C#'s overload resolution rules. This helps framework and library authors guide API usage as they APIs as they develop new and better patterns.

The OverloadResolutionPriorityAttribute can be used in conjunction with the ObsoleteAttribute. A library author may mark properties, methods, types and other programming elements as obsolete, while leaving them in place for backwards compatibility. Using programming elements marked with the ObsoleteAttribute will result in compiler warnings or errors. However, the type or member is still visible to overload resolution and may be selected over a better overload or cause an ambiguity failure. The OverloadResolutionPriorityAttribute lets library authors fix these problems by lowering the priority of obsolete members when there are better alternatives.

Usage

[TestFixture]
public class OverloadResolutionPriorityAttributeTests
{
    [Test]
    public void Run()
    {
        int[] arr = [1, 2, 3];
        //Prints "Span" because resolution priority is higher
        Method(arr);
    }

    [OverloadResolutionPriority(2)]
    static void Method(ReadOnlySpan<int> list) =>
        Console.WriteLine("Span");

    [OverloadResolutionPriority(1)]
    static void Method(int[] list) =>
        Console.WriteLine("Array");
}

snippet source | anchor

UnscopedRefAttribute

Reference: Low Level Struct Improvements

using System.Diagnostics.CodeAnalysis;

struct UnscopedRefUsage
{
    int field;

    [UnscopedRef] ref int Prop1 => ref field;
}

snippet source | anchor

RequiresPreviewFeaturesAttribute

CallerArgumentExpressionAttribute

Reference: CallerArgumentExpression

static class FileUtil
{
    public static void FileExists(string path, [CallerArgumentExpression("path")] string argumentName = "")
    {
        if (!File.Exists(path))
        {
            throw new ArgumentException($"File not found. Path: {path}", argumentName);
        }
    }
}

static class FileUtilUsage
{
    public static string[] Method(string path)
    {
        FileUtil.FileExists(path);
        return File.ReadAllLines(path);
    }
}

snippet source | anchor

InterpolatedStringHandler

References: String Interpolation in C# 10 and .NET 6, Write a custom string interpolation handler

StringSyntaxAttribute

Reference: .NET 7 - The StringSyntaxAttribute

Trimming annotation attributes

Reference: Prepare .NET libraries for trimming

Platform compatibility

Reference: Platform compatibility analyzer

StackTraceHiddenAttribute

Reference: C# – Hide a method from the stack trace

UnmanagedCallersOnly

Reference: Improvements in native code interop in .NET 5.0

SuppressGCTransition

DisableRuntimeMarshalling

Extensions

The class Polyfill includes the following extension methods:

[!IMPORTANT] The methods using AppendInterpolatedStringHandler parameter are not extensions because the compiler prefers to use the overload with string parameter instead.

Extension methods

Boolean

Byte

CancellationToken

CancellationTokenSource

Collections.Concurrent.ConcurrentDictionary<TKey,TValue>

DateOnly

DateTime

DateTimeOffset

Decimal

Dictionary<TKey,TValue>

Double

Guid

HashSet

HttpClient

HttpContent

IDictionary<TKey,TValue>

IEnumerable

IEnumerable

IList

Int16

Int32

Int64

IReadOnlyDictionary<TKey,TValue>

KeyValuePair<TKey,TValue>

List

Process

Random

ReadOnlySpan

ReadOnlySpan

Reflection.EventInfo

Reflection.FieldInfo

Reflection.MemberInfo

Reflection.ParameterInfo

Reflection.PropertyInfo

Regex

SByte

Single

SortedList<TKey,TValue>

Span

Span

Stream

String

StringBuilder

Task

Task

TaskCompletionSource

TextReader

TextWriter

TimeOnly

TimeSpan

Type

UInt16

UInt32

UInt64

Xml.Linq.XDocument

Static helpers

EnumPolyfill

RegexPolyfill

StringPolyfill

BytePolyfill

GuidPolyfill

DateTimePolyfill

DateTimeOffsetPolyfill

DoublePolyfill

IntPolyfill

LongPolyfill

SBytePolyfill

ShortPolyfill

UIntPolyfill

ULongPolyfill

UShortPolyfill

Guard

Lock

TaskCompletionSource

References

If any of the below reference are not included, the related polyfills will be disabled.

System.ValueTuple

If consuming in a project that targets net461 or net462, a reference to System.ValueTuple nuget is required.

<PackageReference Include="System.ValueTuple"
                  Version="4.5.0"
                  Condition="$(TargetFramework.StartsWith('net46'))" />

System.Memory

If using Span APIs and consuming in a project that targets netstandard, netframework, or netcoreapp2*, a reference to System.Memory nuget is required.

<PackageReference Include="System.Memory"
                  Version="4.5.5"
                  Condition="$(TargetFrameworkIdentifier) == '.NETStandard' or
                             $(TargetFrameworkIdentifier) == '.NETFramework' or
                             $(TargetFramework.StartsWith('netcoreapp2'))" />

System.Threading.Tasks.Extensions

If using ValueTask APIs and consuming in a project that target netframework, netstandard2, or netcoreapp2, a reference to System.Threading.Tasks.Extensions nuget is required.

<PackageReference Include="System.Threading.Tasks.Extensions"
                  Version="4.5.4"
                  Condition="$(TargetFramework) == 'netstandard2.0' or
                             $(TargetFramework) == 'netcoreapp2.0' or
                             $(TargetFrameworkIdentifier) == '.NETFramework'" />

Nullability

Example target class

Given the following class

class NullabilityTarget
{
    public string? StringField;
    public string?[] ArrayField;
    public Dictionary<string, object?> GenericField;
}

snippet source | anchor

NullabilityInfoContext

[Test]
public void Test()
{
    var type = typeof(NullabilityTarget);
    var arrayField = type.GetField("ArrayField")!;
    var genericField = type.GetField("GenericField")!;

    var context = new NullabilityInfoContext();

    var arrayInfo = context.Create(arrayField);

    Assert.AreEqual(NullabilityState.NotNull, arrayInfo.ReadState);
    Assert.AreEqual(NullabilityState.Nullable, arrayInfo.ElementType!.ReadState);

    var genericInfo = context.Create(genericField);

    Assert.AreEqual(NullabilityState.NotNull, genericInfo.ReadState);
    Assert.AreEqual(NullabilityState.NotNull, genericInfo.GenericTypeArguments[0].ReadState);
    Assert.AreEqual(NullabilityState.Nullable, genericInfo.GenericTypeArguments[1].ReadState);
}

snippet source | anchor

NullabilityInfoExtensions

Enable by adding and MSBuild property PolyNullability

<PropertyGroup>
  ...
  <PolyNullability>true</PolyNullability>
</PropertyGroup>

NullabilityInfoExtensions provides static and thread safe wrapper around NullabilityInfoContext. It adds three extension methods to each of ParameterInfo, PropertyInfo, EventInfo, and FieldInfo.

Guard

Enable by adding and MSBuild property PolyGuard

<PropertyGroup>
  ...
  <PolyGuard>true</PolyGuard>
</PropertyGroup>

Guard is designed to be a an alternative to the ArgumentException.ThrowIf* APIs added in net7.

With the equivalent Guard APIs:

Polyfills.Guard provides the following APIs:

Guard

Alternatives

PolyShim

https://github.com/Tyrrrz/PolyShim

PolySharp

https://github.com/Sergio0694/PolySharp

Theraot.Core

https://github.com/theraot/Theraot

Combination of

Reason this project was created instead of using the above

PolySharp uses c# source generators. In my opinion a "source-only package" implementation is better because:

The combination of the other 3 packages is not ideal because:

Notes

Icon

Crack designed by Adrien Coquet from The Noun Project.