Ejemplo n.º 1
0
        /// <summary>
        /// Parse the input name and return the quantity object from it.
        /// </summary>
        /// <typeparam name="T">container type of the value</typeparam>
        /// <param name="quantityName"></param>
        /// <returns></returns>
        public static AnyQuantity <T> Parse(string quantityName)
        {
            Type QuantityType = QuantityDimension.QuantityTypeFrom(quantityName);

            if (QuantityType == null)
            {
                throw new QuantityNotFoundException();
            }
            else
            {
                //QuantityType = QuantityType.MakeGenericType(typeof(T));
                //AnyQuantity<T> qty = (AnyQuantity<T>)Activator.CreateInstance(QuantityType);

                AnyQuantity <T> qty = QuantityDimension.QuantityFrom <T>(QuantityDimension.DimensionFrom(QuantityType));
                return(qty);
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }