Repository navigation
Keep the netstandard2.0 public API available on .NET - #1736
tmilnthorp wants to merge 2 commits into
Conversation
A library compiled against the netstandard2.0 assembly can run in an app that loads one of the .NET assemblies, where several public members were missing and calls failed with MissingMethodException: - IQuantityInfo and IQuantityInstanceInfo<TQuantity> were internal on .NET - IQuantityOfType<TQuantity>.QuantityInfo did not exist on .NET - The Temperature tolerance Equals overloads in AffineQuantityExtensions only existed on netstandard2.0 These now exist on all target frameworks. IQuantityOfType.QuantityInfo is obsolete on .NET like IQuantity.QuantityInfo, and is implemented once by IQuantity<TSelf, TUnitType>. Enable package validation to catch such differences in CI. The remaining suppressions are the static abstract members and generic math constraints, which require .NET 7 or later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1736 +/- ##
======================================
Coverage 98% 98%
======================================
Files 515 515
Lines 24092 24093 +1
======================================
+ Hits 23692 23693 +1
Misses 400 400
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
On .NET, calls with Temperature arguments now bind to the Temperature overloads, which left the generic overloads untested. Call them from a generic context to cover them again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
lipchev
left a comment
There was a problem hiding this comment.
The cross-framework fixes work, but there is one compatibility regression to address before approval: existing modern .NET types that implement IQuantityOfType directly acquire a new required getter. The default implementation on IQuantity<TSelf,TUnit> does not cover them. See the inline comment for the proposed fix.
Reviewed b05e9c7 against comparison base 3bf1ef4.
Validation:
- The same compiled netstandard2.0 consumer failed against the base and succeeded against this head on .NET 8, 9 and 10 for quantity metadata and both Temperature tolerance overloads. A separate consumer compiled against the published 6.0.0-pre021 package confirmed the behavior on .NET 10.
- Full local UnitsNet.Tests on .NET 10: base 52,560 passed; head 52,567 passed; 16 skipped each. Both de-CH feet/inches cases passed on both revisions here.
- dotnet pack passed for the head across all four library targets. Enabling validation on the base reproduced 14 errors. The head's nine suppressions are narrowly scoped to the existing static-abstract members and additive-identity constraints; validation also passed with unnecessary suppressions disallowed.
- Concrete Temperature calls now select the non-generic overloads. Comparisons covering mixed units and tolerance edge cases found no behavior change.
The separately tested default-getter proposal resolves the direct-implementer failure without recompiling its existing assembly. Its focused tests passed on .NET 10 (59) and net48 (45), and package validation passed.
The logarithmic instance/static getter mismatch still reproduces and can remain a separate follow-up. Please keep it actively tracked: #1735 was closed for inactivity, not resolved. Once the direct-implementer regression is fixed, these changes can go in independently of that decision.
| #if NET | ||
| [Obsolete("Kept for back-compat with netstandard2.0. On .NET 5+, use the static TSelf.Info property or the GetQuantityInfo() extension method.")] | ||
| #endif | ||
| new IQuantityInstanceInfo<TQuantity> QuantityInfo { get; } |
There was a problem hiding this comment.
[P2] Supply a default getter for direct IQuantityOfType implementations
This adds an abstract instance member to the modern .NET interface. The default at line 153 only protects implementations of IQuantity<TSelf,TUnit>. A type implementing IQuantityOfType directly compiled against the base, but fails to recompile against this head with CS0738. Loading that same unchanged assembly with this head throws TypeLoadException because get_QuantityInfo has no implementation.
On .NET, give this property a default body using the implementation's existing IQuantity metadata:
new IQuantityInstanceInfo<TQuantity> QuantityInfo
=> (IQuantityInstanceInfo<TQuantity>)((IQuantity)this).QuantityInfo;Keep the existing obsolete annotation and the netstandard2.0 abstract declaration. The fuller interface's TSelf.Info override can remain. TQuantity.Info is not available here because TQuantity is constrained only to IQuantity.
I tested this change with an unchanged pre-patch direct implementation: it now loads, returns the expected metadata and successfully calls Create through that metadata. Please add a regression test using a direct implementation that does not supply the new getter; the existing Mass/HowMuch test only covers the fuller interface.
Part of #1735.
Summary
The netstandard2.0 assembly exposed public members that were missing from the .NET assemblies. A netstandard2.0 library calling them failed with
MissingMethodExceptionwhen used from a .NET app. This PR makes those members available on every target framework and turns on package validation so CI catches future differences.Changes
IQuantityInfoandIQuantityInstanceInfo<TQuantity>are now public on all target frameworks. They used to be internal on .NET.IQuantityOfType<TQuantity>.QuantityInfo: now exists on all target frameworks.[Obsolete]with the same message asIQuantity.QuantityInfo, pointing toTSelf.Info.IQuantity<TSelf, TUnitType>implements it once as a default interface member, so generated quantities don't change.TemperaturetoleranceEquals: the twoAffineQuantityExtensionsoverloads were netstandard2.0-only and now exist everywhere. On .NET, calls withTemperaturearguments resolve to these non-generic overloads instead of the generic ones. Both compute the same comparison.<EnablePackageValidation>true</EnablePackageValidation>inUnitsNet.csproj.CompatibilitySuppressions.xmllists the intended exceptions, with a comment explaining them: the static abstract members (Info,Create,From,Zero) and theIAdditiveIdentityconstraints. These require .NET 7+ and can't exist on netstandard2.0.Not in this PR
ILogarithmicQuantity<TSelf>.LogarithmicScalingFactoris still an instance property on netstandard2.0 and a static abstract property on .NET. Package validation doesn't flag it, because it matches members by name. Fixing it means breaking either the netstandard2.0 or the .NET API, so it needs a decision first. #1735 lists the options.Testing
IQuantityOfType<T>.QuantityInfoandTemperature.Equals(other, tolerance):MissingMethodExceptionon master, work with this PR.LogarithmicScalingFactor: still fails, as described above.dotnet packpasses. Before this change it reported 14 errors.QuantityInfo_IQuantityOfTypeReference_ReturnsQuantityInfo.UnitsNet.Testson net10.0: 52,559 passed, 2 failed. The failures areFeetInchesTests.TryParseFeetIncheswithde-CH, which fail the same way on master (see Fix feet/inches parsing with apostrophe grouping #1724).🤖 Generated with Claude Code
https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG