Code Monkey home page Code Monkey logo

polysharp's Introduction

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 polifilled. 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]
    • [UnconditionalSuppressMessage]
  • [StackTraceHidden] (see here)
  • [UnmanagedCallersOnly] (see docs))
  • Platform support annotation attributes (see docs):
    • [ObsoletedOSPlatform]
    • [SupportedOSPlatform]
    • [SupportedOSPlatformGuard]
    • [TargetPlatform]
    • [UnsupportedOSPlatform]
    • [UnsupportedOSPlatformGuard]
  • [SuppressGCTransition] (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.
  • "PolySharpExcludeGeneratedTypes": excludes specific types from generation (';' or ',' separated type names).
  • "PolySharpIncludeGeneratedTypes": only includes specific types for generation (';' or ',' separated type names).

polysharp's People

Contributors

sergio0694 avatar kzu avatar jnyrup avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.