Ejemplo n.º 1
0
        public static T GetEnumValueForOption <T>(SupportedProperty property, string valueToMap)
        {
            var enumType = typeof(T);

            if (!enumType.IsEnum)
            {
                throw new Exception(string.Format("T '{0}' is not of type enum.", enumType.Name));
            }

            Object optionAsEnum;

            if (property.Options == null || property.Options.Count == 0)
            {
                throw new Exception(string.Format("No options are defined for supported property '{0}'.", property.Name));
            }

            if (typeof(T) != property.OptionsEnumType)
            {
                throw new Exception(string.Format("Conversion type '{0}' does not match the supported property option enum type '{1}'.", enumType.Name, property.OptionsEnumType.Name));
            }

            SupportedProperty.ThrowExceptionIfInvalid(property, valueToMap);

            try
            {
                optionAsEnum = Enum.Parse(property.OptionsEnumType, valueToMap == null ? null : valueToMap, true);

                return((T)optionAsEnum);
            }
            catch
            {
                throw new Exception(string.Format("Supported property '{0}' could not convert option '{1}' to enum type '{2}'.", property.Name, valueToMap == null ? "null" : valueToMap, property.OptionsEnumType.Name));
            }
        }
Ejemplo n.º 2
0
        public static void ThrowExceptionIfInvalid(SupportedProperty property, object value)
        {
            if (property.IsRequired && value == null)
            {
                throw new Exception(string.Format("Supported property '{0}' is required.", property.Name));
            }

            switch (property.FieldType)
            {
            case SupportedPropertyType.Integer:

                int propValueAsInt;
                if (!int.TryParse(value.ToString(), out propValueAsInt))
                {
                    throw new Exception(string.Format("Supported property '{0}' with value '{1}' could not be converted to an integer.", property.Name, value));
                }

                if (property.MinValue.HasValue && propValueAsInt < property.MinValue)
                {
                    throw new Exception(string.Format("Supported property '{0}' has a minimum value of {1}.", property.Name, property.MinValue.Value));
                }

                if (property.MaxValue.HasValue && propValueAsInt > property.MaxValue)
                {
                    throw new Exception(string.Format("Supported property '{0}' has a maximum value of {1}.", property.Name, property.MaxValue.Value));
                }

                break;

            case SupportedPropertyType.Text:
                // do nothing
                break;

            case SupportedPropertyType.Options:

                if (property.OptionsEnumType == null)
                {
                    if (property.Options.Where(d => (d == null ? null : d.ToString()) == (value == null ? null : value.ToString())).Count() == 0)
                    {
                        throw new Exception(string.Format("Supported property '{0}' does not support option '{1}'.", property.Name, value == null ? "null" : value));
                    }
                }
                else
                {
                    if (value == null)
                    {
                        throw new Exception("Option values can not be null.");
                    }

                    var enumIsValid = Enum.IsDefined(property.OptionsEnumType, Enum.Parse(property.OptionsEnumType, value.ToString()));

                    if (!enumIsValid)
                    {
                        throw new Exception(string.Format("Supported property option value '{0}' is not valid for enum of type '{1}'.", value.ToString(), property.OptionsEnumType.Name));
                    }
                }

                break;

            case SupportedPropertyType.Boolean:

                bool propValueAsBool;
                if (!bool.TryParse(value.ToString(), out propValueAsBool))
                {
                    throw new Exception(string.Format("Property '{0}' with value '{1}' could not be converted to a Boolean.", property.Name, value));
                }

                break;

            default:
                throw new EnumValueNotImplementedException <SupportedPropertyType>(property.FieldType);
            }
        }