Пример #1
0
        /// <inheritdoc />
        public override void PrepareFieldConfiguration(IFieldConfiguration fieldConfiguration)
        {
            if (!fieldConfiguration.Attributes.Has("step"))
            {
                if (FieldGenerator.IsIntegralNumber())
                {
                    fieldConfiguration.Attr("step", 1);
                }
                else if (FieldGenerator.Metadata.DataTypeName == DataType.Currency.ToString())
                {
                    fieldConfiguration.Attr("step", 0.01);
                }
                else
                {
                    fieldConfiguration.Attr("step", "any");
                }
            }

            if (!fieldConfiguration.Attributes.Has("min") || !fieldConfiguration.Attributes.Has("max"))
            {
                object min = null;
                object max = null;

                if (FieldGenerator.GetCustomAttributes().OfType <RangeAttribute>().Any())
                {
                    var converter = TypeDescriptor.GetConverter(FieldGenerator.GetUnderlyingType());
                    var range     = FieldGenerator.GetCustomAttributes().OfType <RangeAttribute>().First();
                    min = range.Minimum;
                    max = range.Maximum;
                }
                else
                {
                    var type = FieldGenerator.GetUnderlyingType();

                    // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types
                    if (type == typeof(byte))
                    {
                        min = 0;
                        max = 255;
                    }
                    if (type == typeof(sbyte))
                    {
                        min = -128;
                        max = 127;
                    }
                    if (type == typeof(short))
                    {
                        min = -32768;
                        max = 32767;
                    }
                    if (type == typeof(ushort))
                    {
                        min = 0;
                        max = 65535;
                    }
                    if (type == typeof(uint))
                    {
                        min = 0;
                    }
                    if (type == typeof(ulong))
                    {
                        min = 0;
                    }
                }

                if (!fieldConfiguration.Attributes.Has("min") && min != null)
                {
                    fieldConfiguration.Min(min.ToString());
                }
                if (!fieldConfiguration.Attributes.Has("max") && max != null)
                {
                    fieldConfiguration.Max(max.ToString());
                }
            }
        }