Skip to content

Keep the netstandard2.0 public API available on .NET - #1736

Open
tmilnthorp wants to merge 2 commits into
angularsen:masterfrom
tmilnthorp:tmilntho/consistent-tfm-api
Open

tmilnthorp wants to merge 2 commits into
angularsen:masterfrom
tmilnthorp:tmilntho/consistent-tfm-api

Conversation

@tmilnthorp

Copy link
Copy Markdown
Collaborator

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 MissingMethodException when 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

  • Interfaces: IQuantityInfo and IQuantityInstanceInfo<TQuantity> are now public on all target frameworks. They used to be internal on .NET.
  • IQuantityOfType<TQuantity>.QuantityInfo: now exists on all target frameworks.
    • On .NET it's [Obsolete] with the same message as IQuantity.QuantityInfo, pointing to TSelf.Info.
    • IQuantity<TSelf, TUnitType> implements it once as a default interface member, so generated quantities don't change.
  • Temperature tolerance Equals: the two AffineQuantityExtensions overloads were netstandard2.0-only and now exist everywhere. On .NET, calls with Temperature arguments resolve to these non-generic overloads instead of the generic ones. Both compute the same comparison.
  • Package validation: <EnablePackageValidation>true</EnablePackageValidation> in UnitsNet.csproj.
    • CompatibilitySuppressions.xml lists the intended exceptions, with a comment explaining them: the static abstract members (Info, Create, From, Zero) and the IAdditiveIdentity constraints. These require .NET 7+ and can't exist on netstandard2.0.
    • The PR workflow already packs, so validation runs on every PR.

Not in this PR

ILogarithmicQuantity<TSelf>.LogarithmicScalingFactor is 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

  • Cross-framework repro (netstandard2.0 library used from a net10.0 app):
    • IQuantityOfType<T>.QuantityInfo and Temperature.Equals(other, tolerance): MissingMethodException on master, work with this PR.
    • LogarithmicScalingFactor: still fails, as described above.
  • Package validation: a fresh dotnet pack passes. Before this change it reported 14 errors.
  • New test: QuantityInfo_IQuantityOfTypeReference_ReturnsQuantityInfo.
  • Full UnitsNet.Tests on net10.0: 52,559 passed, 2 failed. The failures are FeetInchesTests.TryParseFeetInches with de-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

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

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98%. Comparing base (3bf1ef4) to head (b05e9c7).
⚠️ Report is 4 commits behind head on master.

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           
Flag Coverage Δ
net48 97% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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 lipchev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread UnitsNet/IQuantity.cs
#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; }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[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.

This branch has not been deployed

No deployments
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.

2 participants