public object Convert(object?value, Type targetType, OptionBaseAttribute optionAttribute)
        {
            if (value == null)
            {
                return(ConverterAction.DoNothing);
            }

            if (!targetType.IsAssignableTo(typeof(IEnumerable)))
            {
                return(ConverterAction.DoNothing);
            }

            var itemType = typeof(object);

            if (targetType.IsConstructedGenericType && targetType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                itemType = targetType.GetGenericArguments().First();
            }

            var values = value.ToStringSafe().Split(optionAttribute.Separator);

            var result = values.Select(x => System.Convert.ChangeType(x, itemType)).ToArray();

            return(itemType == typeof(object)
                ? result
                : result.OfType(itemType));
        }
Ejemplo n.º 2
0
        public object Convert(object?value, Type targetType, OptionBaseAttribute optionAttribute)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString()))
            {
                return(true);
            }

            return(bool.TryParse(value.ToString(), out var boolValue) && boolValue);
        }
        public object Convert(object?value, Type targetType, OptionBaseAttribute optionAttribute)
        {
            var text = value?.ToString();

            return(text switch
            {
                "Hello" => "World",
                "World" => "Hello",
                _ => text?.ToUpper() ?? string.Empty
            });
Ejemplo n.º 4
0
        public object?Convert(object?value, Type targetType, OptionBaseAttribute optionAttribute)
        {
            if (targetType != typeof(string) && targetType.IsAssignableTo(typeof(IEnumerable)))
            {
                return(_enumerableConverter.Convert(value, targetType, optionAttribute));
            }

            return(targetType.IsEnum
                ? _enumConverter.Convert(value, targetType, optionAttribute)
                : InternalConvert(value, targetType, optionAttribute));
        }
Ejemplo n.º 5
0
        public object Convert(object?value, Type targetType, OptionBaseAttribute optionAttribute)
        {
            if (!targetType.IsEnum)
            {
                return(ConverterAction.DoNothing);
            }

            var allEnumFields = EnumUtils.GetEnumFieldInfos(targetType);

            return(targetType.GetCustomAttribute <FlagsAttribute>() == null
                ? ConvertEnum(allEnumFields, value)
                : ConvertEnumFlags(allEnumFields, value, targetType));
        }
Ejemplo n.º 6
0
 protected OptionPropertyBase(PropertyInfo propertyInfo, OptionBaseAttribute optionAttribute)
 {
     _propertyInfo    = propertyInfo;
     _optionAttribute = optionAttribute;
 }
Ejemplo n.º 7
0
 private object?InternalConvert(object?value, Type targetType, OptionBaseAttribute optionAttribute)
 {
     return(_converters.TryGetValue(targetType, out var converter)
                     ? converter.Convert(value, targetType, optionAttribute)
                     : System.Convert.ChangeType(value, targetType));
 }
 public RequiredArgumentMissingException(PropertyInfo missingProperty,
                                         OptionBaseAttribute optionAttribute)
 {
     MissingProperty  = missingProperty;
     _optionAttribute = optionAttribute;
 }