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: True
// System.Memory`1: True
// System.Net.Http.HttpContent: True
// System.ReadOnlyMemory`1: True
// System.ReadOnlySpan`1: True
// System.Span`1: True
// System.Threading.Tasks.ValueTask: True
// System.Threading.Tasks.ValueTask`1: True
//
// M:System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd``1(`0,System.Func{`0,``0,`1},``0): False
// M:System.Collections.Generic.CollectionExtensions.GetValueOrDefault``2(System.Collections.Generic.IReadOnlyDictionary{``0,``1},``0): False
// M:System.Collections.Generic.CollectionExtensions.GetValueOrDefault``2(System.Collections.Generic.IReadOnlyDictionary{``0,``1},``0,``1): False
// M:System.Collections.Generic.KeyValuePair`2.Deconstruct(`0@,`1@): False
// M:System.Collections.Generic.Queue`1.TryDequeue(`0@): False
// 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): False
// 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): False
// 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}): False
// M:System.Linq.Enumerable.DistinctBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IEqualityComparer{``1}): False
// M:System.Linq.Enumerable.MaxBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1}): False
// M:System.Linq.Enumerable.MaxBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1}): False
// M:System.Linq.Enumerable.MinBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1}): False
// M:System.Linq.Enumerable.MinBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1}): False
// M:System.Linq.Enumerable.OrderDescending``1(System.Collections.Generic.IEnumerable{``0}): False
// M:System.Linq.Enumerable.OrderDescending``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0}): False
// M:System.Linq.Enumerable.Order``1(System.Collections.Generic.IEnumerable{``0}): False
// M:System.Linq.Enumerable.Order``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0}): False
// M:System.Linq.Enumerable.ToHashSet``1(System.Collections.Generic.IEnumerable{``0}): False
// M:System.Linq.Enumerable.ToHashSet``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IEqualityComparer{``0}): False
// M:System.Linq.Enumerable.Zip``2(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IEnumerable{``1}): False
// 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): False
// 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): False
// M:System.Net.Http.HttpContent.CopyToAsync(System.IO.Stream,System.Threading.CancellationToken): False
// M:System.Net.Http.HttpContent.ReadAsStream(System.Threading.CancellationToken): False
// M:System.Net.Http.HttpContent.ReadAsStream: False
// M:System.String.Contains(System.Char): False
// M:System.String.Contains(System.Char,System.StringComparison): False
// M:System.String.Contains(System.String,System.StringComparison): False
// M:System.String.CopyTo(System.Span{System.Char}): False
// M:System.String.EndsWith(System.Char): False
// M:System.String.GetHashCode(System.StringComparison): False
// M:System.String.IndexOf(System.Char,System.StringComparison): False
// M:System.String.Replace(System.String,System.String,System.StringComparison): False
// M:System.String.ReplaceLineEndings(System.String): False
// M:System.String.ReplaceLineEndings: False
// M:System.String.Split(System.Char,System.Int32,System.StringSplitOptions): False
// M:System.String.Split(System.Char,System.StringSplitOptions): False
// M:System.String.StartsWith(System.Char): False
// 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[]): False
// M:System.Text.StringBuilder.AppendJoin(System.Char,System.String[]): False
// M:System.Text.StringBuilder.AppendJoin(System.String,System.Object[]): False
// M:System.Text.StringBuilder.AppendJoin(System.String,System.String[]): False
// M:System.Text.StringBuilder.AppendJoin``1(System.Char,System.Collections.Generic.IEnumerable{``0}): False
// M:System.Text.StringBuilder.AppendJoin``1(System.String,System.Collections.Generic.IEnumerable{``0}): False
// M:System.Threading.CancellationTokenSource.CancelAsync: True
// M:System.Threading.Tasks.Task.WaitAsync(System.Threading.CancellationToken): False
// T:System.Collections.Generic.PriorityQueue`2: False
// T:System.Collections.Generic.ReferenceEqualityComparer: False
// T:System.Diagnostics.CodeAnalysis.AllowNullAttribute: False
// T:System.Diagnostics.CodeAnalysis.DisallowNullAttribute: False
// T:System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute: False
// T:System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute: False
// T:System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute: False
// T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes: False
// T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute: False
// T:System.Diagnostics.CodeAnalysis.MaybeNullAttribute: False
// T:System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute: False
// T:System.Diagnostics.CodeAnalysis.MemberNotNullAttribute: False
// T:System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute: False
// T:System.Diagnostics.CodeAnalysis.NotNullAttribute: False
// T:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute: False
// T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute: False
// T:System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute: False
// T:System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute: False
// T:System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute: False
// T:System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute: False
// T:System.Diagnostics.CodeAnalysis.StringSyntaxAttribute: False
// T:System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute: False
// T:System.Diagnostics.CodeAnalysis.UnscopedRefAttribute: False
// T:System.Diagnostics.StackTraceHiddenAttribute: False
// T:System.HashCode: False
// T:System.Index: False
// T:System.Net.Http.ReadOnlyMemoryContent: False
// T:System.Range: False
// T:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute: False
// T:System.Runtime.CompilerServices.CallerArgumentExpressionAttribute: False
// T:System.Runtime.CompilerServices.CollectionBuilderAttribute: True
// T:System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute: False
// T:System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute: False
// T:System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute: False
// T:System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute: False
// T:System.Runtime.CompilerServices.IsExternalInit: False
// T:System.Runtime.CompilerServices.ModuleInitializerAttribute: False
// T:System.Runtime.CompilerServices.RequiredMemberAttribute: False
// T:System.Runtime.CompilerServices.SkipLocalsInitAttribute: False
// T:System.Runtime.CompilerServices.TupleElementNamesAttribute: False
// T:System.Runtime.InteropServices.SuppressGCTransitionAttribute: False
// T:System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute: False
// T:System.Runtime.Versioning.ObsoletedOSPlatformAttribute: False
// T:System.Runtime.Versioning.RequiresPreviewFeaturesAttribute: False
// T:System.Runtime.Versioning.SupportedOSPlatformAttribute: False
// T:System.Runtime.Versioning.SupportedOSPlatformGuardAttribute: False
// T:System.Runtime.Versioning.TargetPlatformAttribute: False
// T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute: False
// T:System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute: False
// 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) - 25 other generators

ApparatusAOT

AspectGenerator

CommonCodeGenerator

DudNet

Enhanced.GetTypes

FastGenericNew

HsuSgSync

Immutype

Ling.Audit

Lombok.NET

M31.FluentAPI

MemoryPack

Microsoft.Extensions.Logging

Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator

Microsoft.Interop.JavaScript.JSImportGenerator

OptionToStringGenerator

QueryStringGenerator

RSCG_Decorator

RSCG_UtilityTypes

StaticReflection

SyncMethodGenerator

System.Runtime.InteropServices

System.Text.RegularExpressions

TelemetryLogging

ThisClass