Add netstandard2.0 target for .NET Framework 4.6.2+ support#162
Merged
Add netstandard2.0 target for .NET Framework 4.6.2+ support#162
Conversation
Add netstandard2.0 as a target framework alongside netstandard2.1 and net8.0 using hand-written polyfills (no external polyfill packages). Key changes: - Add netstandard2.0 to PostHog.csproj and TestLibrary.csproj targets - Add Microsoft.Bcl.HashCode as a conditional ns2.0-only dependency - Guard default interface implementations in IFeatureFlagCache with #if !NETSTANDARD2_0 (ns2.0 lacks CLR support for DIM) - Replace Dictionary(IReadOnlyDictionary) constructor calls with ToDictionary() for ns2.0 compatibility - Add polyfills for System.Index, System.Range, string.Contains, string.Replace, string.GetHashCode, Dictionary.TryAdd, IReadOnlyDictionary.GetValueOrDefault, and KeyValuePair.Deconstruct Closes #145
Contributor
posthog-dotnet Compliance ReportDate: 2026-02-26 23:15:20 UTC ✅ All Tests Passed!29/29 tests passed Capture Tests✅ 29/29 tests passed View Details
|
Contributor
There was a problem hiding this comment.
Pull request overview
Adds netstandard2.0 support to the SDK so it can be consumed by .NET Framework 4.6.2+ projects, using in-repo polyfills and conditional dependencies to keep behavior consistent across TFMs.
Changes:
- Add
netstandard2.0to the target frameworks for the main library and TestLibrary. - Introduce
NETSTANDARD2_0-only polyfills for missing BCL APIs (Index/Range, string overloads, dictionary helpers). - Guard default interface implementations and adjust a few call sites for
netstandard2.0compatibility; addMicrosoft.Bcl.HashCodeonly fornetstandard2.0.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/TestLibrary/TestLibrary.csproj | Adds netstandard2.0 target and adjusts conditional package reference. |
| tests/TestLibrary/Fakes/Polyfills/StringPolyfills.cs | Adds a small NETSTANDARD2_0 string polyfill used by tests. |
| tests/TestLibrary/Fakes/FakeLoggerProvider.cs | Replaces dictionary construction to be compatible with older TFMs. |
| tests/TestLibrary/Fakes/FakeHttpMessageHandler.cs | Minor nullability/operator adjustments in request capture logic. |
| src/PostHog/PostHog.csproj | Adds netstandard2.0 target and a conditional Microsoft.Bcl.HashCode dependency. |
| src/PostHog/Library/Polyfills/StringPolyfills.cs | Adds NETSTANDARD2_0 polyfills for missing string overloads. |
| src/PostHog/Library/Polyfills/IndexRangePolyfill.cs | Adds System.Index/System.Range polyfills for C# indices/ranges on netstandard2.0. |
| src/PostHog/Library/Polyfills/DictionaryPolyfills.cs | Adds NETSTANDARD2_0 polyfills for TryAdd, GetValueOrDefault, and Deconstruct. |
| src/PostHog/Features/IFeatureFlagCache.cs | Guards default interface implementations to avoid DIM on netstandard2.0. |
| src/PostHog/Features/Group.cs | Adjusts dictionary copy to be compatible with netstandard2.0. |
| src/PostHog/ErrorTracking/ExceptionPropertiesBuilder.cs | Nullability adjustment for netstandard2.0 annotation gaps. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Guard against empty oldValue in StringPolyfills.Replace to prevent infinite loop, add clarifying comments for null-forgiving operators in FakeHttpMessageHandler and the DIM conditional compilation pattern in IFeatureFlagCache.
In Release builds, JIT optimizations strip source file paths from async state machine stack frames, causing assertions on filename and context_line to fail. Guard these assertions on whether source info is actually present, and skip the IO-lock test when no source path is available.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
netstandard2.0as a target framework alongsidenetstandard2.1andnet8.0, enabling the SDK on .NET Framework 4.6.2+ projectsMeziantou.Polyfillor other external source generators)Microsoft.Bcl.HashCodeas a conditional ns2.0-only dependencyPicks up the work from #147 (author stopped responding) with a different approach — extending the existing polyfill infrastructure instead of introducing an external polyfill package.
Polyfills added (
#if NETSTANDARD2_0only)System.Index/System.Range— enables C# 8 index/range syntax (^1,..)string.Contains(string, StringComparison),string.Replace(string, string?, StringComparison),string.GetHashCode(StringComparison)Dictionary<K,V>.TryAdd,IReadOnlyDictionary<K,V>.GetValueOrDefault,KeyValuePair<K,V>.DeconstructOther changes
IFeatureFlagCacheguarded with#if !NETSTANDARD2_0(ns2.0 lacks CLR support for DIM). All known implementors already provide explicit implementations.Dictionary(IReadOnlyDictionary)constructor calls replaced withToDictionary()for ns2.0 compatibility[DoesNotReturnIf]/[NotNullWhen]on ns2.0Closes #145
Test plan
dotnet buildsucceeds for all three targets (ns2.0, ns2.1, net8.0) with 0 warningsdotnet test— all existing tests passdotnet pack— NuGet package contains all three TFM foldersMicrosoft.Bcl.HashCodeonly appears as a dependency for ns2.0 consumers