Skip to content

Added polyfills and simplified preprocessor directives#966

Merged
michelebastione merged 12 commits into
mini-software:masterfrom
michelebastione:various-optimizations
May 24, 2026
Merged

Added polyfills and simplified preprocessor directives#966
michelebastione merged 12 commits into
mini-software:masterfrom
michelebastione:various-optimizations

Conversation

@michelebastione
Copy link
Copy Markdown
Collaborator

This PR brings many QOL improvements to improve the readability of the codebase, namely:

  • Added Stream.DisposeAsync polyfill alongside a StreamConfiguredAsyncDisposable (mirroring the runtimes's ConfiguredAsyncDisposable). This allowed clearing most of the preprocessor directives throughout codebase
  • Simplified the remaining directives between #if NETCOREAPP2_0 and #if NET8_0 to the simpler #if NET
  • Added Dictionary<TKey, TValue>.TryAdd, XDocument.LoadAsync and XDocument.SaveAsync polyfills
  • Added more constants to Schemas and Excel.Filenames, and substituted them where they had been prevoiusly hardcoded
  • Removed obsolete FillTemplate method overloads from OpenXmlTemplater
  • Other minor refactorings

Renamed classes and variables, moved Excel data types to own static class, added new values to ExcelContentTypes, Schemas and ExcelFileNames, improved consistency of rels indexing
Changed all directives between `#if NETCOREAPP2_0` and `#if NET8_0` to the simpler `#if NET` to reflect the project's minimum target framework of .NET8
Context, OldXmlReader and NewXmlWriter are now protected instead of private, so that they need to be assigned only in the `SheetStyleBuilderBase` class.
This polyfill, alongside the `StreamConfiguredAsyncDisposable` struct that copies the behaviour of the runtime's `ConfiguredAsyncDisposable` and the `Stream.ConfigureAwait` method that resturns it, allowed for cleaning all of the preprocessor directives that were used throughout whole codebase to dispose synchronously of streams for the netstandard2.0 (which was most of them).
This also added the benefit of allowing for their asynchronous disposal in framework versions not expicitly targeted by checking if they implement the `IAsyncDisposable` interface.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modernizes the library by centralizing OpenXml constants, simplifying preprocessor directives (often replacing specific version checks with a general #if NET block), and improving asynchronous resource handling through broader use of await using and ConfigureAwait(false). It also introduces several polyfills to enable modern patterns on .NET Standard 2.0. However, the review identified multiple regression risks where the switch to #if NET (covering .NET 5.0+) would break builds on older .NET versions for APIs introduced in .NET 6 or 7, such as Task.WaitAsync, StreamWriter.FlushAsync, and TextReader.ReadLineAsync. Other issues include a potential NullReferenceException in the StreamConfiguredAsyncDisposable polyfill and a missing compatibility check for Stream.CopyToAsync in .NET Standard 2.0.

Comment thread src/MiniExcel.Core/Helpers/Polyfills.cs Outdated
Comment thread src/MiniExcel.Core/Helpers/MiniExcelStreamWriter.cs
Comment thread src/MiniExcel.Csv/CsvReader.cs
Comment thread src/MiniExcel.OpenXml/FluentMapping/MappingTemplateApplicator.cs
Comment thread src/MiniExcel.OpenXml/OpenXmlReader.cs
@michelebastione michelebastione merged commit e6590a3 into mini-software:master May 24, 2026
3 checks passed
@michelebastione michelebastione deleted the various-optimizations branch May 24, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant