Ejemplo n.º 1
0
        private static EnumValue[] GetValues([NotNull] Type enumType)
        {
            var result     = new List <EnumValue>();
            var actualType = enumType.ToNullableUnderlying();

            var fields = ReflectionEnumHelper.GetFields(actualType)
                         .ToDictionary(m => m.Name, StringComparer.Ordinal);
            var enumNames  = Enum.GetNames(enumType);
            var enumValues = Enum.GetValues(enumType).Cast <object>();

            foreach (var(name, value) in enumNames.Zip(enumValues, (name, value) => (name, value)))
            {
                var field            = fields[name];
                var displayAttribute = field.GetCustomAttribute <DisplayAttribute>();

                result.Add(
                    new EnumValue(
                        name,
                        (Enum)value,
                        field,
                        displayAttribute?.GetName(),
                        displayAttribute?.GetDescription()));
            }

            return(result.ToArray());
        }
Ejemplo n.º 2
0
        private static MetricUnit[] GetMetricUnits(Type metricEnumType)
        {
            var result = new List <MetricUnit>();
            var fields = ReflectionEnumHelper.GetFields(metricEnumType)
                         .OrderBy(f => Convert.ToDouble(f.GetValue(null), CultureInfo.InvariantCulture));

            MetricUnit previousUnit = null;

            foreach (var field in fields)
            {
                var unit = GetMetricUnit(field);

                if (previousUnit != null)
                {
                    Code.AssertArgument(
                        previousUnit.AppliesFrom < unit.AppliesFrom,
                        nameof(metricEnumType),
                        $"The applies from value of {metricEnumType.Name}.{unit.EnumValue} ({unit.AppliesFrom.ToInvariantString()}) " +
                        $"should be greater than prev unit {metricEnumType.Name}.{previousUnit.EnumValue} value ({previousUnit.AppliesFrom.ToInvariantString()})");
                }
                previousUnit = unit;

                result.Add(unit);
            }

            Code.AssertArgument(
                result.Count > 0,
                nameof(metricEnumType),
                $"The enum {metricEnumType} should be not empty.");

            return(result.ToArray());
        }