/// <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> /// 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); } } } }
/// <summary> /// Sets the display unit. /// </summary> /// <typeparam name="T"> /// The quantity type. /// </typeparam> /// <param name="unitProvider"> /// The unit provider. /// </param> /// <param name="symbol"> /// The unit symbol (must be registered). /// </param> /// <returns> /// <c>true</c> if the unit was set, <c>false</c> otherwise /// </returns> public static bool TrySetDisplayUnit <T>(this IUnitProvider unitProvider, string symbol) where T : IQuantity <T> { return(unitProvider.TrySetDisplayUnit(typeof(T), symbol)); }