PolySharp 1.13.1
See the version list below for details.
dotnet add package PolySharp --version 1.13.1
NuGet\Install-Package PolySharp -Version 1.13.1
<PackageReference Include="PolySharp" Version="1.13.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add PolySharp --version 1.13.1
#r "nuget: PolySharp, 1.13.1"
// Install PolySharp as a Cake Addin #addin nuget:?package=PolySharp&version=1.13.1 // Install PolySharp as a Cake Tool #tool nuget:?package=PolySharp&version=1.13.1
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:
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 unlessSystem.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:
- Nullability attributes (for nullable reference types):
[AllowNull]
[DisallowNull]
[DoesNotReturn]
[DoesNotReturnIf]
[MaybeNull]
[MaybeNullWhen]
[MemberNotNull]
[MemberNotNullWhen]
[NotNull]
[NotNullIfNotNull]
[NotNullWhen]
Index
andRange
(see indices and ranges)[UnscopedRef]
(see low-level struct improvements)- Required members (see required modifier)
[RequiredMember]
[SetsRequiredMembers]
[CompilerFeatureRequired]
(needed to support several features)[IsExternalInit]
(needed for init-only properties)[SkipLocalsInit]
(see docs)- Interpolated string handlers (see docs)
[InterpolatedStringHandler]
[InterpolatedStringHandlerArgument]
[CallerArgumentExpression]
(see docs)[RequiresPreviewFeatures]
(needed for preview features)[AsyncMethodBuilder]
(needed for custom method builder types)[StringSyntax]
(needed to enable syntax highlight in the IDE)[ModuleInitializer]
(needed to enable custom module initializers)
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.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (27)
Showing the top 5 NuGet packages that depend on PolySharp:
Package | Downloads |
---|---|
AdaptiveCards.Templating
This library implements a JSON-to-JSON templating/data-binding engine. While it is designed to be used with Adaptive Cards, it is not dependent on Adaptive Cards and can therefore be used in many contexts and applications. |
|
TinyIpc
.NET inter process broadcast message bus. |
|
Freshdesk.Api
A lightweight wrapper around the Freshdesk REST API as described in https://developers.freshdesk.com/api |
|
TinyLogger
Tiny extendable logger that plugins into Microsoft.Extensions.Logging |
|
K4os.Xpovoc.Core
Simple job scheduler for .NET/.NET Core |
GitHub repositories (132)
Showing the top 5 popular GitHub repositories that depend on PolySharp:
Repository | Stars |
---|---|
files-community/Files
A modern file manager that helps users organize their files and folders.
|
|
restsharp/RestSharp
Simple REST and HTTP API Client for .NET
|
|
spectreconsole/spectre.console
A .NET library that makes it easier to create beautiful console applications.
|
|
unoplatform/uno
Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
|
|
lepoco/wpfui
WPF UI provides the Fluent experience in your known and loved WPF framework. Intuitive design, themes, navigation and new immersive controls. All natively and effortlessly.
|
Version | Downloads | Last updated |
---|---|---|
1.15.0 | 38,993 | 11/21/2024 |
1.14.1 | 2,215,202 | 12/18/2023 |
1.14.0 | 46,121 | 11/28/2023 |
1.13.2 | 700,182 | 5/16/2023 |
1.13.1 | 104,609 | 4/6/2023 |
1.12.1 | 350,141 | 1/21/2023 |
1.10.0 | 427,823 | 1/15/2023 |
1.9.0 | 2,332 | 1/14/2023 |
1.8.3 | 13,002 | 1/9/2023 |
1.8.2 | 189 | 1/8/2023 |
1.8.1 | 23,816 | 11/22/2022 |
1.8.0 | 848 | 11/16/2022 |
1.7.1 | 29,167 | 10/29/2022 |
1.7.0 | 173 | 10/28/2022 |
1.6.0 | 375 | 10/26/2022 |
1.5.0 | 169 | 10/26/2022 |
1.4.0 | 171 | 10/25/2022 |
1.3.0 | 337 | 10/23/2022 |
1.2.0 | 160 | 10/22/2022 |
1.1.0 | 158 | 10/22/2022 |
1.0.1 | 158 | 10/22/2022 |
1.0.0 | 4,649 | 10/22/2022 |