/// <summary> /// Create the unit directly from the specfied dimension based on the unit system given. /// </summary> /// <param name="dimension"></param> public static Unit DiscoverUnit(QuantityDimension dimension, string unitSystem) { List <Unit> SubUnits = new List <Unit>(); if (dimension.Currency.Exponent != 0) { Unit u = new Currency.Coin(); u.UnitExponent = dimension.Currency.Exponent; u.UnitDimension = new QuantityDimension { Currency = new DimensionDescriptors.CurrencyDescriptor(dimension.Currency.Exponent) }; SubUnits.Add(u); } if (dimension.Mass.Exponent != 0) { Type UnitType = Unit.GetDefaultUnitTypeOf(typeof(Mass <>), unitSystem); Unit u = (Unit)Activator.CreateInstance(UnitType); u.UnitExponent = dimension.Mass.Exponent; u.UnitDimension = new QuantityDimension(dimension.Mass.Exponent, 0, 0); SubUnits.Add(u); } if (dimension.Length.Exponent != 0) { Type UnitType = Unit.GetDefaultUnitTypeOf(typeof(Length <>), unitSystem); Unit u = (Unit)Activator.CreateInstance(UnitType); u.UnitExponent = dimension.Length.Exponent; u.UnitDimension = new QuantityDimension() { Length = dimension.Length }; SubUnits.Add(u); } if (dimension.Time.Exponent != 0) { Type UnitType = Unit.GetDefaultUnitTypeOf(typeof(Time <>), unitSystem); Unit u = (Unit)Activator.CreateInstance(UnitType); u.UnitExponent = dimension.Time.Exponent; u.UnitDimension = new QuantityDimension() { Time = dimension.Time }; SubUnits.Add(u); } if (dimension.Temperature.Exponent != 0) { Type UnitType = Unit.GetDefaultUnitTypeOf(typeof(Temperature <>), unitSystem); Unit u = (Unit)Activator.CreateInstance(UnitType); u.UnitExponent = dimension.Temperature.Exponent; u.UnitDimension = new QuantityDimension() { Temperature = dimension.Temperature }; SubUnits.Add(u); } if (dimension.LuminousIntensity.Exponent != 0) { Type UnitType = Unit.GetDefaultUnitTypeOf(typeof(LuminousIntensity <>), unitSystem); Unit u = (Unit)Activator.CreateInstance(UnitType); u.UnitExponent = dimension.LuminousIntensity.Exponent; u.UnitDimension = new QuantityDimension() { LuminousIntensity = dimension.LuminousIntensity }; SubUnits.Add(u); } if (dimension.AmountOfSubstance.Exponent != 0) { Type UnitType = Unit.GetDefaultUnitTypeOf(typeof(AmountOfSubstance <>), unitSystem); Unit u = (Unit)Activator.CreateInstance(UnitType); u.UnitExponent = dimension.AmountOfSubstance.Exponent; u.UnitDimension = new QuantityDimension(0, 0, 0, 0, 0, dimension.AmountOfSubstance.Exponent, 0); SubUnits.Add(u); } if (dimension.ElectricCurrent.Exponent != 0) { Type UnitType = Unit.GetDefaultUnitTypeOf(typeof(ElectricalCurrent <>), unitSystem); Unit u = (Unit)Activator.CreateInstance(UnitType); u.UnitExponent = dimension.ElectricCurrent.Exponent; u.UnitDimension = new QuantityDimension() { ElectricCurrent = dimension.ElectricCurrent }; SubUnits.Add(u); } if (dimension.Currency.Exponent != 0) { Unit u = new Currency.Coin(); u.UnitExponent = dimension.Currency.Exponent; u.UnitDimension = u.UnitDimension * dimension.Currency.Exponent; SubUnits.Add(u); } if (dimension.Digital.Exponent != 0) { Unit u = new Digital.Bit(); u.UnitExponent = dimension.Digital.Exponent; u.UnitDimension = u.UnitDimension * dimension.Digital.Exponent; SubUnits.Add(u); } Unit un = null; try { Type qType = QuantityDimension.QuantityTypeFrom(dimension); un = new Unit(qType, SubUnits.ToArray()); } catch (QuantityNotFoundException) { un = new Unit(null, SubUnits.ToArray()); } return(un); }
/// <summary> /// Construct a unit based on the quantity type in SI Base units. /// Any Dimensionless quantity will return in its unit. /// </summary> /// <param name="quantityType"></param> public Unit(Type quantityType) { SubUnits = new List <Unit>(); //try direct mapping first to get the unit Type InnerUnitType = Unit.GetDefaultSIUnitTypeOf(quantityType); if (InnerUnitType == null) { QuantityDimension dimension = QuantityDimension.DimensionFrom(quantityType); if (dimension.Mass.Exponent != 0) { Unit u = new Metric.SI.Gram(); u.UnitExponent = dimension.Mass.Exponent; u.UnitDimension = u.UnitDimension * dimension.Mass.Exponent; SubUnits.Add(u); } if (dimension.Length.Exponent != 0) { Unit u = new Metric.SI.Metre(); u.UnitExponent = dimension.Length.Exponent; u.UnitDimension = u.UnitDimension * dimension.Length.Exponent; SubUnits.Add(u); } if (dimension.Time.Exponent != 0) { Unit u = new Shared.Second(); u.UnitExponent = dimension.Time.Exponent; u.UnitDimension = u.UnitDimension * dimension.Time.Exponent; SubUnits.Add(u); } if (dimension.Temperature.Exponent != 0) { Unit u = new Metric.SI.Kelvin(); u.UnitExponent = dimension.Temperature.Exponent; u.UnitDimension = u.UnitDimension * dimension.Temperature.Exponent; SubUnits.Add(u); } if (dimension.LuminousIntensity.Exponent != 0) { Unit u = new Metric.SI.Candela(); u.UnitExponent = dimension.LuminousIntensity.Exponent; u.UnitDimension = u.UnitDimension * dimension.LuminousIntensity.Exponent; SubUnits.Add(u); } if (dimension.AmountOfSubstance.Exponent != 0) { Unit u = new Metric.SI.Mole(); u.UnitExponent = dimension.AmountOfSubstance.Exponent; u.UnitDimension = u.UnitDimension * dimension.AmountOfSubstance.Exponent; SubUnits.Add(u); } if (dimension.ElectricCurrent.Exponent != 0) { Unit u = new Metric.SI.Ampere(); u.UnitExponent = dimension.ElectricCurrent.Exponent; u.UnitDimension = u.UnitDimension * dimension.ElectricCurrent.Exponent; SubUnits.Add(u); } if (dimension.Currency.Exponent != 0) { Unit u = new Currency.Coin(); u.UnitExponent = dimension.Currency.Exponent; u.UnitDimension = u.UnitDimension * dimension.Currency.Exponent; SubUnits.Add(u); } if (dimension.Digital.Exponent != 0) { Unit u = new Digital.Bit(); u.UnitExponent = dimension.Digital.Exponent; u.UnitDimension = u.UnitDimension * dimension.Digital.Exponent; SubUnits.Add(u); } } else { //subclass of AnyQuantity //use direct mapping with the exponent of the quantity Unit un = (Unit)Activator.CreateInstance(InnerUnitType); SubUnits.Add(un); } this._Symbol = GenerateUnitSymbolFromSubBaseUnits(); this._IsDefaultUnit = true; //the quantity may be derived quantity which shouldn't be referenced :check here. this._QuantityType = quantityType; _UnitDimension = QuantityDimension.DimensionFrom(this._QuantityType); this._IsBaseUnit = false; }