Beispiel #1
0
        public IUnitMetaConfig ConvertValueBased(Unit second, Func <decimal, decimal> firstToSecond, Func <decimal, decimal> secondToFirst)
        {
            if (PhysicalUnit == null)
            {
                PhysicalUnit = unit.DerivePhysicalUnitsFromConstituentParts();
            }
            if (PhysicalUnit.IsDimensionless())
            {
                throw new InvalidOperationException("You must define physical unit of the left-hand side");
            }

            var unitMeta = second.GetUnitData();

            if (unitMeta == null || unitMeta.PhysicalUnit.IsDimensionless())
            {
                // If right-hand side of conversion has no physical unit, we'll assume that it has the same physical unit as left-hand.
                unitMeta = (UnitMeta)config.Unit(second).IsPhysicalUnit(PhysicalUnit);
            }

            if (unitMeta.PhysicalUnit != PhysicalUnit)
            {
                throw new InvalidOperationException("You can only define conversions between units that are compatible as physical units");
            }

            var node = config.UnitGraph.AddUnit(second);

            ConversionInfo.AddConversion(node, firstToSecond);
            node.AddConversion(ConversionInfo, secondToFirst);
            return(this);
        }
Beispiel #2
0
        IUnitScale IUnitScale.To(Unit singleUnit, int scale)
        {
            var newUnit = config.Unit(singleUnit)
                          .IsPhysicalUnit(precedingUnit.PhysicalUnit)
                          .ConvertValueBased(precedingUnit.Unit, v => v * scale, v => v / scale);

            ((IUnitMetaConfig)precedingUnit).ConvertValueBased(singleUnit, v => v / scale, v => v * scale);
            precedingUnit = (UnitMeta)newUnit;
            return(this);
        }