Пример #1
0
        /// <summary>
        /// Creates an unit system from the given prototype. This can be useful when
        /// e.g. extending one of the standard unit systems with custom units.
        /// If the prototype is an UnitSystem the created unit system will be an
        /// identical copy of the unit system. If not, only units that are convertible
        /// to the base unit will be added.
        /// </summary>
        /// <param name="prototype"></param>
        /// <returns></returns>
        public static UnitSystem CreateFrom(IUnitSystem prototype)
        {
            if (prototype is UnitSystem)
            {
                return((UnitSystem)((UnitSystem)prototype).Clone());
            }

            UnitSystem system = new UnitSystem();

            foreach (IDimension dimension in prototype.Dimensions)
            {
                system.AddDimension(dimension);

                // Register base unit
                IUnit baseUnit = prototype.GetBaseUnit(dimension);
                system.AddBaseUnit(baseUnit);

                // Register scaled units
                foreach (IUnit unit in prototype.GetSupportedUnits(dimension))
                {
                    if (!ReferenceEquals(unit, baseUnit) && prototype.CanConvert(baseUnit, unit))
                    {
                        system.AddScaledUnit(baseUnit, unit, prototype.CreateConverter(baseUnit, unit));
                    }
                }
            }
            return(system);
        }