/// <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); } } } }