• Stars
    star
    1,731
  • Rank 26,926 (Top 0.6 %)
  • Language
    C#
  • License
    MIT License
  • Created about 2 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PolySharp provides generated, source-only polyfills for C# language features, to easily use all runtime-agnostic features downlevel. Add a reference, set your C# version to latest and have fun! ๐Ÿš€

image
.NET NuGet NuGet

What is it? ๐Ÿš€

PolySharp provides generated, source-only polyfills for C# language features, to easily use all runtime-agnostic features downlevel. The package is distributed as a source generator, so that it will automatically detect which polyfills are needed depending on the target framework and project in use: just add a reference to PolySharp, set your C# language version to latest, and have fun!

TLDR? What is this for? โœจ

Put simply: are you working on .NET Framework, or UWP, or some other older .NET runtime and still would like to use all the cool new features that C# 11 has? Well this library lets you do just that! It will generate for you all the "magic types" that the C# compiler needs to "see" in order for it to allow using new language features even if you're not using the latest framework out there.

Here's an example of some of the new features that PolySharp can enable downlevel:

image

Note: not all the new C# features can be "tricked" this way (eg. those requiring runtime support, such as static abstract members, still won't work). But almost everything else will (eg. nullability annotations, pattern matching, etc.), and this applies to a big number of new C# features. Try PolySharp out, don't get stuck on C# 6.0 or other older versions! ๐ŸŽ‰

Note: use on .NET Framework 3.5 is particularly limited due to shortcomings of the BCL there. In particular, the System.Range type will not be generated unless System.ValueTuple is referenced (meaning that eg. list patterns won't work by default), and some features such as records will not be usable at all due to the C# compiler missing some necessary APIs that cannot be polyfilled. All other features should work as expected.

Documentation ๐Ÿ“–

PolySharp includes the following polyfills:

To leverage them, make sure to bump your C# language version. You can do this by setting the <LangVersion> MSBuild property in your project. For instance, by adding <LangVersion>11.0</LangVersion> (or your desired C# version) to the first <PropertyGroup> of your .csproj file. For more info on this, see here, but remember that you don't need to manually copy polyfills anymore: simply adding a reference to PolySharp will do this for you automatically.

It also includes the following optional runtime-supported polyfills:

  • Reflection annotation attributes (see docs):
    • [DynamicallyAccessedMembers]
    • [DynamicDependency]
    • [RequiresUnreferencedCode]
    • [RequiresDynamicCode]
    • [UnconditionalSuppressMessage]
    • [RequiresAssemblyFiles]
  • [StackTraceHidden] (see here)
  • [UnmanagedCallersOnly] (see docs))
  • Platform support annotation attributes (see docs):
    • [ObsoletedOSPlatform]
    • [SupportedOSPlatform]
    • [SupportedOSPlatformGuard]
    • [TargetPlatform]
    • [UnsupportedOSPlatform]
    • [UnsupportedOSPlatformGuard]
  • [SuppressGCTransition] (see here)
  • [DisableRuntimeMarshalling] (see here)

Options โš™๏ธ

PolySharp's generation can be configured through some MSBuild properties to set in consuming projects.

The following properties are available:

  • "PolySharpUsePublicAccessibilityForGeneratedTypes": makes all generated types public.
  • "PolySharpIncludeRuntimeSupportedAttributes": enables polyfills for (dummy) runtime-supported attributes too.
  • "PolySharpUseInteropServices2NamespaceForUnmanagedCallersOnlyAttribute": moves [UnmanagedCallersOnly].
  • "PolySharpExcludeGeneratedTypes": excludes specific types from generation (';' or ',' separated type names).
  • "PolySharpIncludeGeneratedTypes": only includes specific types for generation (';' or ',' separated type names).
  • "PolySharpExcludeTypeForwardedToDeclarations": never generates any [TypeForwardedTo] declarations.

More Repositories

1

ComputeSharp

A .NET library to run C# code in parallel on the GPU through DX12, D2D1, and dynamically generated HLSL compute and pixel shaders, with the goal of making GPU computing easy to use for all .NET developers! ๐Ÿš€
C#
2,690
star
2

NeuralNetwork.NET

A TensorFlow-inspired neural network library built from scratch in C# 7.3 for .NET Standard 2.0, with GPU support through cuDNN
C#
540
star
3

BinaryPack

The fastest and most memory efficient binary serialization library for .NET Standard 2.1, powered by dynamic IL generation
C#
331
star
4

UICompositionAnimations

A powerful and easy to use library to work with Composition and XAML animations and Win2D effects
C#
91
star
5

Brainf_ckSharp

A complete and full-featured Brainf_ck IDE/console for Windows 10 (UWP), with a high-performance REPL interpreter
C#
56
star
6

ReflectionToIL

A demonstration and benchmark of different approaches to load closure fields using reflection and dynamic code generation
C#
30
star
7

privatelike

An attribute, generator and analyzer to declare "private-like" types in C# (internal but producing errors if not used as if they were declared as private)
C#
10
star
8

BigWatson

A complete, ligthweight and self-contained .NET Standard 2.0 offline exception logging library, powered by Realm
C#
9
star
9

quickup

A .NET Core 2.1 CLI tool to quickly create one-way backups from one folder to another
C#
5
star
10

Win2DSample

A simple and minimal Win2D sample in an unpackaged Win32 app
C#
4
star
11

GarbageCollectorC

A simple, multi-thread implementation of a GC in C
C
3
star
12

List_T

A parametric list in C with supports with LINQ-style functions and lambda expressions
C
3
star
13

clup

A no-nonsense .NET Core 2.1 CLI duplicate files remover
C#
2
star
14

FizzBuzz

The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows: "Write a program that prints the numbers from 1 to 100. But for multiples of three print โ€œFizzโ€ instead of the number and for the multiples of five print โ€œBuzzโ€. For numbers which are multiples of both three and five print โ€œFizzBuzzโ€."
PowerShell
1
star
15

Snake

The classic arcade game Snake written in C
C
1
star
16

sepconv-gan

A TensorFlow-based network to perform video frame interpolation
Python
1
star