Comments (4)
Merged and should be out as nuget shortly.
Release UnitsNet/5.30.0 · angularsen/UnitsNet
from unitsnet.
Hi, what version are you on? I see you are using QuantityType
and it was removed in v5.
I updated your code to v5, but it still fails.
I believe it boils down to casting to Enum
and the lookup fails as shown in CastToEnum_Fails()
, but GenericEnum_Ok()
passes.
using System;
using Xunit;
namespace UnitsNet.Tests;
public class GasFlowTests
{
public GasFlowTests()
{
UnitAbbreviationsCache.Default.MapUnitToDefaultAbbreviation(GasFlowRateUnit.StandardCubicMetersPerDay, "m³/d");
UnitAbbreviationsCache.Default.MapUnitToDefaultAbbreviation(GasFlowRateUnit.ThousandStandardCubicFeetPerDay, "kft³/d");
}
[Fact]
public void GenericEnum_Ok()
{
Assert.Equal("m³/d", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(GasFlowRateUnit.StandardCubicMetersPerDay));
}
[Fact]
public void CastToEnum_Fails()
{
Assert.Equal("m³/d", UnitAbbreviationsCache.Default.GetDefaultAbbreviation((Enum)GasFlowRateUnit.StandardCubicMetersPerDay));
}
public enum GasFlowRateUnit
{
StandardCubicMetersPerDay = 1,
ThousandStandardCubicFeetPerDay = 2,
}
}
System.ArgumentException: Type provided must be an Enum.
System.ArgumentException
Type provided must be an Enum.
at System.Enum.GetEnumInfo(RuntimeType enumType, Boolean getNames)
at System.RuntimeType.GetEnumName(Object value)
at UnitsNet.UnitAbbreviationsCache.TryGetUnitAbbreviations(Type unitType, Int32 unitValue, IFormatProvider formatProvider, String[]& abbreviations) in C:\dev\unitsnet\UnitsNet\CustomCode\UnitAbbreviationsCache.cs:line 246
at UnitsNet.UnitAbbreviationsCache.GetUnitAbbreviations(Type unitType, Int32 unitValue, IFormatProvider formatProvider) in C:\dev\unitsnet\UnitsNet\CustomCode\UnitAbbreviationsCache.cs:line 231
at UnitsNet.UnitAbbreviationsCache.GetDefaultAbbreviation(Type unitType, Int32 unitValue, IFormatProvider formatProvider) in C:\dev\unitsnet\UnitsNet\CustomCode\UnitAbbreviationsCache.cs:line 204
at UnitsNet.UnitAbbreviationsCache.GetDefaultAbbreviation[TUnitType](TUnitType unit, IFormatProvider formatProvider) in C:\dev\unitsnet\UnitsNet\CustomCode\UnitAbbreviationsCache.cs:line 190
at UnitsNet.Tests.GasFlowTests.CastToEnum() in C:\dev\unitsnet\UnitsNet.Tests\GasFlowTests.cs:line 26
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
I took a quick look and found that the lookup implementation incorrectly tries to do typeof()
on the generic type, but when passing Enum
that type information is lost.
A fix is on the way in #1302.
from unitsnet.
Hi @angularsen! thanks for your answer I'm using 4.132.0
from unitsnet.
@farenasmz are you able to try the latest v5 nuget and see if that helps? You may have to migrate some usages.
https://github.com/angularsen/UnitsNet/wiki/Upgrading-from-4.x-to-5.x
from unitsnet.
Related Issues (20)
- Custom units incompatible with decimal format? HOT 3
- Temperature.Equals method uses the wrong Unit for the tolerance parameter HOT 4
- ToAutoUnitString HOT 3
- Wrong Plural for MeterPerMinute Speed Units Names HOT 1
- Include yard-based density units HOT 4
- Automatically retrieve Type based on Unit HOT 4
- New Unit - Grade (Slope) HOT 12
- Add Molality units HOT 1
- Insulin concentration HOT 1
- Units of time on Mars HOT 3
- Brackets missing when there are multiple denominators in units string HOT 1
- Bad conversion from base unit 0.1 (example: deciliter to liter) for decimal number. HOT 1
- Protobuf-Net support not working because of DataMember order 0 HOT 1
- Accuracy Issue when converting from CubicMeters to CubicInches HOT 2
- Possible issues with the IComparable interface and associated operators HOT 8
- Quantity.Equals(Quantity other, Quantity tolerance) gives false negative due to floating-point arithmetic HOT 1
- Add type safety and improve type inference in `UnitsMath.cs` HOT 6
- v4.117.0 breaks quantity comparison between different units HOT 3
- Support Cross-Technologies DTO & API Specifications
- NuGets license: MIT instead of MIT-0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unitsnet.