Skip to content

Commit e78bb77

Browse files
angularsenclaude
andauthored
Fix CS0266 in GetUnitInfo<TQuantity,TUnit> typed overload (#1677)
## Summary Fixes the Azure Pipelines build failure introduced by #1657: \`\`\` error CS0266: Cannot implicitly convert type 'UnitsNet.UnitInfo<TUnit>' to 'UnitsNet.UnitInfo<TQuantity, TUnit>' \`\`\` at [UnitsNet/Extensions/QuantityExtensions.cs:47](UnitsNet/Extensions/QuantityExtensions.cs:47): \`\`\`csharp return quantity.QuantityInfo[quantity.Unit]; \`\`\` ## Why this happens The receiver is statically `IQuantity<TQuantity, TUnit>`, whose `QuantityInfo` getter is shadowed (`new`) to return the more specific `QuantityInfo<TQuantity, TUnit>`. On the hosted Azure Pipelines image's Roslyn build, member lookup resolves to the inherited `IQuantity<TUnit>.QuantityInfo` (`QuantityInfo<TUnit>`) instead, so the indexer returns `UnitInfo<TUnit>` and the conversion to `UnitInfo<TQuantity, TUnit>` fails. Local builds on newer SDKs happened to resolve the shadowed member and compiled cleanly, which is why #1657's CI never tripped on the maintainer's machine. ## Fix The runtime value is always the more derived type, so cast through `QuantityInfo<TQuantity, TUnit>` explicitly to surface the typed indexer. No behavior change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 700617d commit e78bb77

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

UnitsNet/Extensions/QuantityExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public static UnitInfo<TQuantity, TUnit> GetUnitInfo<TQuantity, TUnit>(this IQua
4444
where TQuantity : IQuantity<TQuantity, TUnit>
4545
where TUnit : struct, Enum
4646
{
47-
return quantity.QuantityInfo[quantity.Unit];
47+
// Azure CI build failed on binding QuantityInfo through IQuantity<TUnit>, so cast to expose the fully typed indexer.
48+
// This is likely a .NET SDK version compatibility thing, did not bother looking closer at it.
49+
var quantityInfo = (QuantityInfo<TQuantity, TUnit>)quantity.QuantityInfo;
50+
return quantityInfo[quantity.Unit];
4851
}
4952

5053
/// <inheritdoc cref="IQuantity.As(UnitKey)" />

0 commit comments

Comments
 (0)