/// <summary>
 /// Changes to the specified culture.
 /// </summary>
 /// <param name="provider">The unit provider.</param>
 /// <returns>The disposable default unit provider object.</returns>
 public static IDisposable TemporaryChangeTo(IUnitProvider provider)
 {
     var cc = new DefaultUnitProvider { previousProvider = UnitProvider.Default };
     UnitProvider.Default = provider;
     System.Diagnostics.Debug.WriteLine("Set UnitProvider to " + provider);
     return cc;
 }
Example #2
0
        /// <summary>
        /// Tries to parse the specified string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The format provider.</param>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if the string was parsed, <c>false</c> otherwise.</returns>
        public static bool TryParse(string input, IFormatProvider provider, IUnitProvider unitProvider, out ElectricResistance result)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.TryParse(input, provider, out result));
        }
        /// <summary>
        /// Tries to parse the specified string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The format provider.</param>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if the string was parsed, <c>false</c> otherwise.</returns>
        public static bool TryParse(string input, IFormatProvider provider, IUnitProvider unitProvider, out HeatFluxDensity result)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.TryParse(input, provider, out result));
        }
Example #4
0
        /// <summary>
        /// Tries to parse the specified string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The format provider.</param>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if the string was parsed, <c>false</c> otherwise.</returns>
        public static bool TryParse(string input, IFormatProvider provider, IUnitProvider unitProvider, out MassConcentration result)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.TryParse(input, provider, out result));
        }
        /// <summary>
        /// Tries to parse the specified string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The format provider.</param>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if the string was parsed, <c>false</c> otherwise.</returns>
        public static bool TryParse(string input, IFormatProvider provider, IUnitProvider unitProvider, out VolumetricWaterContent result)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.TryParse(input, provider, out result));
        }
Example #6
0
        /// <summary>
        /// Tries to parse the specified string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The format provider.</param>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if the string was parsed, <c>false</c> otherwise.</returns>
        public static bool TryParse(string input, IFormatProvider provider, IUnitProvider unitProvider, out LinearMomentum result)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.TryParse(input, provider, out result));
        }
Example #7
0
        /// <summary>
        /// Tries to parse the specified string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The format provider.</param>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if the string was parsed, <c>false</c> otherwise.</returns>
        public static bool TryParse(string input, IFormatProvider provider, IUnitProvider unitProvider, out RotationalStiffness result)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.TryParse(input, provider, out result));
        }
Example #8
0
        /// <summary>
        /// Tries to parse the specified string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The format provider.</param>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if the string was parsed, <c>false</c> otherwise.</returns>
        public static bool TryParse(string input, IFormatProvider provider, IUnitProvider unitProvider, out TemperatureRateOfChange result)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.TryParse(input, provider, out result));
        }
Example #9
0
        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <param name="format">
        /// The format.
        /// </param>
        /// <param name="formatProvider">
        /// The format provider.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider.
        /// </param>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public string ToString(string format, IFormatProvider formatProvider, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = formatProvider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.Format(format, formatProvider, this));
        }
        /// <summary>
        /// Tries to parse the specified string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The format provider.</param>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if the string was parsed, <c>false</c> otherwise.</returns>
        public static bool TryParse(string input, IFormatProvider provider, IUnitProvider unitProvider, out TypographicLength result)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            return(unitProvider.TryParse(input, provider, out result));
        }
        /// <summary>
        /// Changes to the specified culture.
        /// </summary>
        /// <param name="provider">The unit provider.</param>
        /// <returns>The disposable default unit provider object.</returns>
        public static IDisposable TemporaryChangeTo(IUnitProvider provider)
        {
            var cc = new DefaultUnitProvider {
                previousProvider = UnitProvider.Default
            };

            UnitProvider.Default = provider;
            System.Diagnostics.Debug.WriteLine("Set UnitProvider to " + provider);
            return(cc);
        }
Example #12
0
 /// <summary>
 /// Registers the units in the specified assembly.
 /// </summary>
 /// <param name="unitProvider">
 /// The unit provider.
 /// </param>
 /// <param name="assembly">
 /// The assembly.
 /// </param>
 public static void RegisterUnits(this IUnitProvider unitProvider, Assembly assembly)
 {
     foreach (var t in assembly.ExportedTypes)
     {
         if (typeof(IQuantity).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
         {
             unitProvider.RegisterUnits(t);
         }
     }
 }
Example #13
0
        /// <summary>
        /// Parses a string.
        /// </summary>
        /// <typeparam name="T">The type.</typeparam>
        /// <param name="unitProvider">The unit provider.</param>
        /// <param name="input">The input string.</param>
        /// <param name="provider">The numeric format provider.</param>
        /// <param name="unit">The unit (output).</param>
        /// <returns>True if the parsing was successful.</returns>
        public static bool TryParse <T>(this IUnitProvider unitProvider, string input, IFormatProvider provider, out T unit)
        {
            if (!unitProvider.TryParse(typeof(T), input, provider, out var quantity))
            {
                unit = default(T);
                return(false);
            }

            unit = (T)quantity;
            return(true);
        }
Example #14
0
        /// <summary>
        /// Gets the unit that matches the specified name.
        /// </summary>
        /// <typeparam name="T">
        /// The unit type.
        /// </typeparam>
        /// <param name="unitProvider">
        /// The unit provider.
        /// </param>
        /// <param name="symbol">
        /// The unit symbol.
        /// </param>
        /// <param name="unit">
        /// The unit.
        /// </param>
        /// <returns>
        /// <c>true</c> if the unit name was found, <c>false</c> otherwise
        /// </returns>
        public static bool TryGetUnit <T>(this IUnitProvider unitProvider, string symbol, out T unit)
            where T : IQuantity <T>
        {
            if (unitProvider.TryGetUnit(typeof(T), symbol, out var u))
            {
                unit = (T)u;
                return(true);
            }

            unit = default(T);
            return(false);
        }
Example #15
0
        /// <summary>
        /// Gets the display unit for the specified type.
        /// </summary>
        /// <typeparam name="T">
        /// The unit type.
        /// </typeparam>
        /// <param name="unitProvider">
        /// The unit provider.
        /// </param>
        /// <param name="unit">
        /// The unit.
        /// </param>
        /// <param name="unitSymbol">
        /// The unit symbol.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> .
        /// </returns>
        public static bool TryGetDisplayUnit <T>(this IUnitProvider unitProvider, out T unit, out string unitSymbol)
        {
            IQuantity quantity;

            if (!unitProvider.TryGetDisplayUnit(typeof(T), out quantity, out unitSymbol))
            {
                unit = default(T);
                return(false);
            }

            unit = (T)quantity;
            return(true);
        }
Example #16
0
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="Temperature"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static Temperature Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            if (!unitProvider.TryParse(input, unitProvider.Culture, out Temperature value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
Example #17
0
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert. 
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="ElectricDipole"/> that represents the quantity in <paramref name="input" />. 
        /// </returns>
        public static ElectricDipole Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            ElectricDipole value;
            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return value;
        }
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert. 
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="RadiationEquivalentDose"/> that represents the quantity in <paramref name="input" />. 
        /// </returns>
        public static RadiationEquivalentDose Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            RadiationEquivalentDose value;
            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return value;
        }
Example #19
0
        /// <summary>
        /// Converts a string representation of a quantity in a specific culture-specific format with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="provider">
        /// An object that supplies culture-specific formatting information about <paramref name="input" />. If not specified, the culture of the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="Velocity"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static Velocity Parse(string input, IFormatProvider provider = null, IUnitProvider unitProvider = null)
        {
            if (unitProvider == null)
            {
                unitProvider = provider as IUnitProvider ?? UnitProvider.Default;
            }

            Velocity value;

            if (!unitProvider.TryParse(input, provider, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
Example #20
0
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="AngularFrequency"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static AngularFrequency Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            AngularFrequency value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
Example #21
0
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="FirstMomentOfArea"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static FirstMomentOfArea Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            FirstMomentOfArea value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
        /// <summary>
        /// Registers the unit properties in the specified type.
        /// </summary>
        /// <param name="unitProvider">
        /// The unit provider.
        /// </param>
        /// <param name="type">
        /// The type.
        /// </param>
        public static void RegisterUnits(this IUnitProvider unitProvider, Type type)
        {
            foreach (var property in type.GetProperties(BindingFlags.Static | BindingFlags.Public))
            {
                foreach (UnitAttribute ua in property.GetCustomAttributes(typeof(UnitAttribute), false))
                {
                    unitProvider.RegisterUnit((IQuantity)property.GetValue(null, null), ua.Symbol);

                    if (ua.IsDefaultDisplayUnit)
                    {
                        var unit = (IQuantity)property.GetValue(null, null);
                        unitProvider.TrySetDisplayUnit(unit.GetType(), ua.Symbol);
                    }
                }
            }
        }
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="AmountOfSubstance"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static AmountOfSubstance Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            AmountOfSubstance value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="TypographicResolution"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static TypographicResolution Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            TypographicResolution value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="VelocitySquared"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static VelocitySquared Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            VelocitySquared value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="MassConcentrationInWater"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static MassConcentrationInWater Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            MassConcentrationInWater value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
Example #27
0
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="CustomLength"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static CustomLength Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            CustomLength value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="LuminousIntensity"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static LuminousIntensity Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            LuminousIntensity value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="VolumetricWaterContent"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static VolumetricWaterContent Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            VolumetricWaterContent value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
        /// <summary>
        /// Registers the unit properties in the specified type.
        /// </summary>
        /// <param name="unitProvider">
        /// The unit provider.
        /// </param>
        /// <param name="type">
        /// The type.
        /// </param>
        public static void RegisterUnits(this IUnitProvider unitProvider, Type type)
        {
            foreach (var property in type.GetTypeInfo().DeclaredProperties)
            {
                foreach (var ua in property.GetCustomAttributes(typeof(UnitAttribute), false).Cast <UnitAttribute>())
                {
                    unitProvider.RegisterUnit((IQuantity)property.GetValue(null, null), ua.Symbol);

                    if (ua.IsDefaultDisplayUnit)
                    {
                        var unit = (IQuantity)property.GetValue(null, null);
                        unitProvider.TrySetDisplayUnit(unit.GetType(), ua.Symbol);
                    }
                }
            }
        }
Example #31
0
        /// <summary>
        /// Converts a string representation of a quantity with a specific unit provider.
        /// </summary>
        /// <param name="input">
        /// A string that contains the quantity to convert.
        /// </param>
        /// <param name="unitProvider">
        /// The unit provider. If not specified, the default <see cref="UnitProvider" /> is used.
        /// </param>
        /// <returns>
        /// A <see cref="RotationalStiffness"/> that represents the quantity in <paramref name="input" />.
        /// </returns>
        public static RotationalStiffness Parse(string input, IUnitProvider unitProvider)
        {
            if (unitProvider == null)
            {
                unitProvider = UnitProvider.Default;
            }

            RotationalStiffness value;

            if (!unitProvider.TryParse(input, unitProvider.Culture, out value))
            {
                throw new FormatException("Invalid format.");
            }

            return(value);
        }
 public QuantityJsonConverter(IUnitProvider unitProvider)
 {
     this.unitProvider = unitProvider;
 }