Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

PolySharp

> 编程语言
Open source

PolySharp provides generated, source-only polyfills for C# language features, to easily use all runtime-agnostic features downlevel. Add a reference, set your C

2.2K stars0 likes0 views
WebsiteGitHub

About

PolySharp provides generated, source-only polyfills for C# language features, to easily use all runtime-agnostic features downlevel. Add a reference, set your C


[](https://www.nuget.org/stats/packages/PolySharp?groupby=Version) [](https://www.nuget.org/packages/PolySharp/)

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# 14 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 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:

  • Nullability attributes (for nullable reference types):
    • [AllowNull]
    • [DisallowNull]
    • [DoesNotReturn]
    • [DoesNotReturnIf]
    • [MaybeNull]
    • [MaybeNullWhen]
    • [MemberNotNull]
    • [MemberNotNullWhen]
    • [NotNull]
    • [NotNullIfNotNull]
    • [NotNullWhen]
  • Index and Range (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)
  • [RequiresLocation] (needed to enable ref readonly parameters)
  • [CollectionBuilder] (needed for collection expressions)
  • [Experimental] (needed for experimental features)
  • [OverloadResolutionPriority] (needed for overload resolution priority)
  • [ParamsCollection] (needed for params collection)
  • [ConstantExpected] (see proposal)
  • [CompilerLoweringPreserve] (see proposal)
  • [ExtensionMarker] (needed for extensions)

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>14.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)
  • [UnsafeAccessor] (see here)
  • [InlineArray] (see here)
  • [DisableUserUnhandledExceptions] (see here)
  • Attribute model for feature switches with trimming support (see docs):
    • [FeatureGuard]
    • [FeatureSwitchDefinition]
  • [WasmImportLinkage] (see here)
  • [UnsafeAccessorType] (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.
  • "PolySharpUseEmbeddedAttributeForGeneratedTypes": adds the [Embedded] attributes to generated types.
  • "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.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#csharpdotnetdotnet-coredotnet-framework

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言