Skip to main content

Meziantou.Polyfill by Gérald Barré

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: Meziantou.Polyfill

Source Generator to help multi-targeting projects.

Author: Gérald Barré

NuGet: https://www.nuget.org/packages/Meziantou.Polyfill/

You can find more details at https://www.meziantou.net/polyfills-in-dotnet-to-ease-multi-targeting.htm

Source : https://github.com/meziantou/Meziantou.Polyfill

Original Readme

note

Meziantou.Polyfill

Source Generator that adds polyfill methods and types. This helps working with multi-targeted projects.

You can use the following MSBuild properties to configure which polyfills are generated:

<PropertyGroup>
<!-- semi-column separated list of name prefix -->
<!-- Tip: The name of the generated polyfills are available in the generated "Debug.g.cs" file -->
<MeziantouPolyfill_IncludedPolyfills>T:Type1|T:Type2|M:Member1</MeziantouPolyfill_IncludedPolyfills>
<MeziantouPolyfill_ExcludedPolyfills>M:System.Diagnostics.</MeziantouPolyfill_ExcludedPolyfills>

<!-- Optional: Output the generated files to the obj\GeneratedFiles folder -->
<EmitCompilerGeneratedFiles>True</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

How to add a new polyfill

  • Create a new file named <xml documentation id>.cs in the project Meziantou.Polyfill.Editor
  • Run Meziantou.Polyfill.Generator

Notes:

  • All files must be self contained. Use a file class if needed.
  • If you need to generate a file only when another polyfill is generated, add // when <xml documentation id> in the file

About

note

Generating polyfills that you can see source without de-compiling

How to use

Example ( source csproj, source files )

This is the CSharp Project that references Meziantou.Polyfill

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Polyfill" Version="1.0.28">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// IncludedMembers: <null>
// ExcludedMembers: <null>
// System.Collections.Immutable.ImmutableArray`1: False
// System.Memory`1: False
// System.Net.Http.HttpContent: True
// System.ReadOnlyMemory`1: False
// System.ReadOnlySpan`1: False
// System.Span`1: False
// System.Threading.Tasks.ValueTask: False
// System.Threading.Tasks.ValueTask`1: False
//
// M:System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd``1(`0,System.Func{`0,``0,`1},``0): True
// M:System.Collections.Generic.CollectionExtensions.GetValueOrDefault``2(System.Collections.Generic.IReadOnlyDictionary{``0,``1},``0): True
// M:System.Collections.Generic.CollectionExtensions.GetValueOrDefault``2(System.Collections.Generic.IReadOnlyDictionary{``0,``1},``0,``1): True
// M:System.Collections.Generic.KeyValuePair`2.Deconstruct(`0@,`1@): True
// M:System.Collections.Generic.Queue`1.TryDequeue(`0@): True
// M:System.Collections.Immutable.ImmutableArray`1.AsSpan(System.Int32,System.Int32): False
// M:System.Collections.Immutable.ImmutableArray`1.AsSpan(System.Range): False
// M:System.Diagnostics.Process.WaitForExitAsync(System.Threading.CancellationToken): True
// M:System.IO.Stream.Read(System.Span{System.Byte}): False
// M:System.IO.Stream.ReadAsync(System.Memory{System.Byte},System.Threading.CancellationToken): False
// M:System.IO.Stream.ReadAtLeast(System.Span{System.Byte},System.Int32,System.Boolean): False
// M:System.IO.Stream.ReadAtLeastAsync(System.Memory{System.Byte},System.Int32,System.Boolean,System.Threading.CancellationToken): False
// M:System.IO.Stream.Write(System.ReadOnlySpan{System.Byte}): False
// M:System.IO.Stream.WriteAsync(System.ReadOnlyMemory{System.Byte},System.Threading.CancellationToken): False
// M:System.IO.StreamReader.ReadLineAsync(): False
// M:System.IO.StreamReader.ReadLineAsync(System.Threading.CancellationToken): False
// M:System.IO.TextReader.ReadAsync(System.Memory{System.Char},System.Threading.CancellationToken): False
// M:System.IO.TextReader.ReadToEndAsync(System.Threading.CancellationToken): True
// M:System.IO.TextWriter.WriteAsync(System.ReadOnlyMemory{System.Char},System.Threading.CancellationToken): False
// M:System.Linq.Enumerable.DistinctBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1}): True
// M:System.Linq.Enumerable.DistinctBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IEqualityComparer{``1}): True
// M:System.Linq.Enumerable.MaxBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1}): True
// M:System.Linq.Enumerable.MaxBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1}): True
// M:System.Linq.Enumerable.MinBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1}): True
// M:System.Linq.Enumerable.MinBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1}): True
// M:System.Linq.Enumerable.OrderDescending``1(System.Collections.Generic.IEnumerable{``0}): True
// M:System.Linq.Enumerable.OrderDescending``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0}): True
// M:System.Linq.Enumerable.Order``1(System.Collections.Generic.IEnumerable{``0}): True
// M:System.Linq.Enumerable.Order``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0}): True
// M:System.Linq.Enumerable.ToHashSet``1(System.Collections.Generic.IEnumerable{``0}): True
// M:System.Linq.Enumerable.ToHashSet``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IEqualityComparer{``0}): True
// M:System.Linq.Enumerable.Zip``2(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IEnumerable{``1}): True
// M:System.MemoryExtensions.AsSpan(System.String,System.Int32,System.Int32): False
// M:System.MemoryExtensions.Contains``1(System.ReadOnlySpan{``0},``0): False
// M:System.MemoryExtensions.Contains``1(System.Span{``0},``0): False
// M:System.Net.Http.HttpContent.CopyTo(System.IO.Stream,System.Net.TransportContext,System.Threading.CancellationToken): True
// M:System.Net.Http.HttpContent.CopyToAsync(System.IO.Stream): False
// M:System.Net.Http.HttpContent.CopyToAsync(System.IO.Stream,System.Net.TransportContext): False
// M:System.Net.Http.HttpContent.CopyToAsync(System.IO.Stream,System.Net.TransportContext,System.Threading.CancellationToken): True
// M:System.Net.Http.HttpContent.CopyToAsync(System.IO.Stream,System.Threading.CancellationToken): True
// M:System.Net.Http.HttpContent.ReadAsStream(System.Threading.CancellationToken): True
// M:System.Net.Http.HttpContent.ReadAsStream: True
// M:System.String.Contains(System.Char): True
// M:System.String.Contains(System.Char,System.StringComparison): True
// M:System.String.Contains(System.String,System.StringComparison): True
// M:System.String.CopyTo(System.Span{System.Char}): False
// M:System.String.EndsWith(System.Char): True
// M:System.String.GetHashCode(System.StringComparison): True
// M:System.String.IndexOf(System.Char,System.StringComparison): True
// M:System.String.Replace(System.String,System.String,System.StringComparison): True
// M:System.String.ReplaceLineEndings(System.String): True
// M:System.String.ReplaceLineEndings: True
// M:System.String.Split(System.Char,System.Int32,System.StringSplitOptions): True
// M:System.String.Split(System.Char,System.StringSplitOptions): True
// M:System.String.StartsWith(System.Char): True
// M:System.String.TryCopyTo(System.Span{System.Char}): False
// M:System.Text.Encoding.GetString(System.ReadOnlySpan{System.Byte}): False
// M:System.Text.StringBuilder.Append(System.ReadOnlyMemory{System.Char}): False
// M:System.Text.StringBuilder.Append(System.ReadOnlySpan{System.Char}): False
// M:System.Text.StringBuilder.AppendJoin(System.Char,System.Object[]): True
// M:System.Text.StringBuilder.AppendJoin(System.Char,System.String[]): True
// M:System.Text.StringBuilder.AppendJoin(System.String,System.Object[]): True
// M:System.Text.StringBuilder.AppendJoin(System.String,System.String[]): True
// M:System.Text.StringBuilder.AppendJoin``1(System.Char,System.Collections.Generic.IEnumerable{``0}): True
// M:System.Text.StringBuilder.AppendJoin``1(System.String,System.Collections.Generic.IEnumerable{``0}): True
// M:System.Threading.CancellationTokenSource.CancelAsync: True
// M:System.Threading.Tasks.Task.WaitAsync(System.Threading.CancellationToken): True
// T:System.Collections.Generic.PriorityQueue`2: True
// T:System.Collections.Generic.ReferenceEqualityComparer: True
// T:System.Diagnostics.CodeAnalysis.AllowNullAttribute: True
// T:System.Diagnostics.CodeAnalysis.DisallowNullAttribute: True
// T:System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute: True
// T:System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute: True
// T:System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute: True
// T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes: True
// T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute: True
// T:System.Diagnostics.CodeAnalysis.MaybeNullAttribute: True
// T:System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute: True
// T:System.Diagnostics.CodeAnalysis.MemberNotNullAttribute: True
// T:System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute: True
// T:System.Diagnostics.CodeAnalysis.NotNullAttribute: True
// T:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute: True
// T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute: True
// T:System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute: True
// T:System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute: True
// T:System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute: True
// T:System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute: True
// T:System.Diagnostics.CodeAnalysis.StringSyntaxAttribute: True
// T:System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute: True
// T:System.Diagnostics.CodeAnalysis.UnscopedRefAttribute: True
// T:System.Diagnostics.StackTraceHiddenAttribute: True
// T:System.HashCode: True
// T:System.Index: True
// T:System.Net.Http.ReadOnlyMemoryContent: False
// T:System.Range: True
// T:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute: True
// T:System.Runtime.CompilerServices.CallerArgumentExpressionAttribute: True
// T:System.Runtime.CompilerServices.CollectionBuilderAttribute: True
// T:System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute: True
// T:System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute: True
// T:System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute: True
// T:System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute: True
// T:System.Runtime.CompilerServices.IsExternalInit: True
// T:System.Runtime.CompilerServices.ModuleInitializerAttribute: True
// T:System.Runtime.CompilerServices.RequiredMemberAttribute: True
// T:System.Runtime.CompilerServices.SkipLocalsInitAttribute: True
// T:System.Runtime.CompilerServices.TupleElementNamesAttribute: False
// T:System.Runtime.InteropServices.SuppressGCTransitionAttribute: True
// T:System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute: True
// T:System.Runtime.Versioning.ObsoletedOSPlatformAttribute: True
// T:System.Runtime.Versioning.RequiresPreviewFeaturesAttribute: True
// T:System.Runtime.Versioning.SupportedOSPlatformAttribute: True
// T:System.Runtime.Versioning.SupportedOSPlatformGuardAttribute: True
// T:System.Runtime.Versioning.TargetPlatformAttribute: True
// T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute: True
// T:System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute: True
// T:System.Threading.Tasks.TaskToAsyncResult: True
// T:System.ValueTuple: False
// T:System.ValueTuple`1: False
// T:System.ValueTuple`2: False
// T:System.ValueTuple`3: False
// T:System.ValueTuple`4: False
// T:System.ValueTuple`5: False
// T:System.ValueTuple`6: False
// T:System.ValueTuple`7: False
// T:System.ValueTuple`8: False
// T:System.ITupleInternal: False

Usefull

Download Example (.NET C# )

Share Meziantou.Polyfill

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Meziantou.Polyfill

In the same category (EnhancementClass) - 24 other generators

ApparatusAOT

AspectGenerator

CommonCodeGenerator

CopyTo

DudNet

FastGenericNew

GeneratorEquals

HsuSgSync

Immutype

Ling.Audit

Lombok.NET

M31.FluentAPI

MemoryPack

Microsoft.Extensions.Logging

Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator

Microsoft.Interop.JavaScript.JSImportGenerator

OptionToStringGenerator

RSCG_Decorator

RSCG_UtilityTypes

StaticReflection

SyncMethodGenerator

System.Runtime.InteropServices

System.Text.RegularExpressions

TelemetryLogging