Ejemplo n.º 1
0
        public override object Convert(object source, Type sourceType, Type targetType)
        {
            if (source == null)
            {
                return(null);
            }

            var targetElementType = ConversionUtils.GetElementType(targetType);

            if (targetElementType == null)
            {
                throw new InvalidOperationException("No target element type");
            }

            var enumerable = source as IEnumerable;
            var len        = ConversionUtils.Count(enumerable);

            var array = Array.CreateInstance(targetElementType, len);
            var i     = 0;

            foreach (var sourceElement in enumerable)
            {
                var targetElement = _conversionService.Convert(sourceElement, sourceElement.GetType(), targetElementType);
                array.SetValue(targetElement, i++);
            }

            return(array);
        }
Ejemplo n.º 2
0
        public override object Convert(object source, Type sourceType, Type targetType)
        {
            if (source == null)
            {
                return(null);
            }

            var sourceString      = source as string;
            var fields            = sourceString.Split(_delimit, StringSplitOptions.RemoveEmptyEntries);
            var targetElementType = ConversionUtils.GetElementType(targetType);

            var list = ConversionUtils.CreateCompatListFor(targetType);

            if (list == null)
            {
                throw new InvalidOperationException("Unable to create compatable list");
            }

            for (var i = 0; i < fields.Length; i++)
            {
                var sourceElement = fields[i];
                var targetElement = _conversionService.Convert(sourceElement.Trim(), sourceType, targetElementType);
                list.Add(targetElement);
            }

            return(list);
        }
 private bool CanConvertValue(Type sourceType, Type targetType)
 {
     return(ConversionUtils.CanConvertElements(
                ConversionUtils.GetDictionaryValueType(sourceType),
                ConversionUtils.GetDictionaryValueType(targetType),
                _conversionService));
 }
Ejemplo n.º 4
0
        public override bool Matches(Type sourceType, Type targetType)
        {
            if (sourceType == typeof(string) && ConversionUtils.CanCreateCompatListFor(targetType))
            {
                return(ConversionUtils.CanConvertElements(sourceType, ConversionUtils.GetElementType(targetType), _conversionService));
            }

            return(false);
        }
Ejemplo n.º 5
0
        public override bool Matches(Type sourceType, Type targetType)
        {
            if (!targetType.IsArray || sourceType.IsArray)
            {
                return(false);
            }

            return(ConversionUtils.CanConvertElements(
                       ConversionUtils.GetElementType(sourceType), ConversionUtils.GetElementType(targetType), _conversionService));
        }
Ejemplo n.º 6
0
        private static MethodInfo DetermineToMethod(Type targetClass, Type sourceClass)
        {
            if (typeof(string) == targetClass || typeof(string) == sourceClass)
            {
                // Do not accept a ToString() method or any to methods on String itself
                return(null);
            }

            var method = ConversionUtils.GetMethodIfAvailable(sourceClass, "To" + targetClass.Name);

            return(method != null && !method.IsStatic && targetClass.IsAssignableFrom(method.ReturnType) ? method : null);
        }
Ejemplo n.º 7
0
        public override object Convert(object source, Type sourceType, Type targetType)
        {
            string asString = source as string;

            if (string.IsNullOrEmpty(asString))
            {
                return(null);
            }

            targetType = ConversionUtils.GetNullableElementType(targetType);
            return(Enum.Parse(targetType, asString.Trim(), true));
        }
        public override object Convert(object source, Type sourceType, Type targetType)
        {
            if (source == null)
            {
                return(null);
            }

            var sourceDict = source as IDictionary;
            var len        = sourceDict.Count;

            // Shortcut if possible...
            var copyRequired = !targetType.IsInstanceOfType(source);

            if (!copyRequired && len == 0)
            {
                return(source);
            }

            var targetKeyType   = ConversionUtils.GetDictionaryKeyType(targetType);
            var targetValueType = ConversionUtils.GetDictionaryValueType(targetType);
            var sourceKeyType   = ConversionUtils.GetDictionaryKeyType(sourceType);
            var sourceValueType = ConversionUtils.GetDictionaryValueType(sourceType);

            var dict = ConversionUtils.CreateCompatDictionaryFor(targetType);

            if (dict == null)
            {
                throw new InvalidOperationException("Unable to create compatable dictionary");
            }

            if (!targetType.IsGenericType)
            {
                var enumerator = sourceDict.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    dict.Add(enumerator.Key, enumerator.Value);
                }
            }
            else
            {
                var enumerator = sourceDict.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var targetKey   = ConvertKey(enumerator.Key, sourceKeyType, targetKeyType);
                    var targetValue = ConvertValue(enumerator.Value, sourceValueType, targetValueType);
                    dict.Add(targetKey, targetValue);
                }
            }

            return(dict);
        }
Ejemplo n.º 9
0
        public override bool Matches(Type sourceType, Type targetType)
        {
            // NO OP check Arrays already implement IList, etc.
            if (targetType.IsAssignableFrom(sourceType))
            {
                return(false);
            }

            if (sourceType.IsArray && ConversionUtils.CanCreateCompatListFor(targetType))
            {
                return(ConversionUtils.CanConvertElements(
                           ConversionUtils.GetElementType(sourceType), ConversionUtils.GetElementType(targetType), _conversionService));
            }

            return(false);
        }
        public override object Convert(object source, Type sourceType, Type targetType)
        {
            var sourceCollection = source as IEnumerable;

            if (sourceCollection == null)
            {
                return(null);
            }

            var list = ConversionUtils.CreateCompatListFor(targetType);

            if (list == null)
            {
                throw new InvalidOperationException("Unable to create compatible list");
            }

            if (!targetType.IsGenericType)
            {
                foreach (var elem in sourceCollection)
                {
                    list.Add(elem);
                }
            }
            else
            {
                var targetElementType = ConversionUtils.GetElementType(targetType) ?? typeof(object);
                foreach (var sourceElement in sourceCollection)
                {
                    if (sourceElement != null)
                    {
                        var targetElement = _conversionService.Convert(sourceElement, sourceElement.GetType(), targetElementType);
                        list.Add(targetElement);
                    }
                    else
                    {
                        list.Add(sourceElement);
                    }
                }
            }

            return(list);
        }
Ejemplo n.º 11
0
        private static MethodInfo DetermineFactoryMethod(Type targetClass, Type sourceClass)
        {
            if (typeof(string) == targetClass)
            {
                // Do not accept the String.valueOf(Object) method
                return(null);
            }

            var method = ConversionUtils.GetStaticMethod(targetClass, "ValueOf", sourceClass);

            if (method == null)
            {
                method = ConversionUtils.GetStaticMethod(targetClass, "Of", sourceClass);
                if (method == null)
                {
                    method = ConversionUtils.GetStaticMethod(targetClass, "From", sourceClass);
                }
            }

            return(method);
        }
Ejemplo n.º 12
0
        public override object Convert(object source, Type sourceType, Type targetType)
        {
            if (source == null)
            {
                return(null);
            }

            var elementDesc = ConversionUtils.GetElementType(targetType);
            var target      = ConversionUtils.CreateCompatListFor(targetType);

            if (elementDesc == null)
            {
                target.Add(source);
            }
            else
            {
                var singleElement = _conversionService.Convert(source, sourceType, elementDesc);
                target.Add(singleElement);
            }

            return(target);
        }
Ejemplo n.º 13
0
        public override object Convert(object source, Type sourceType, Type targetType)
        {
            var asArray = source as Array;

            if (asArray == null)
            {
                return(null);
            }

            var len = asArray.GetLength(0);
            var arrayElementType = ConversionUtils.GetElementType(asArray.GetType());

            var list = ConversionUtils.CreateCompatListFor(targetType);

            if (list == null)
            {
                throw new InvalidOperationException("Unable to create compatible list");
            }

            if (!targetType.IsGenericType)
            {
                for (var i = 0; i < len; i++)
                {
                    list.Add(asArray.GetValue(i));
                }
            }
            else
            {
                var targetElementType = ConversionUtils.GetElementType(targetType);
                for (var i = 0; i < len; i++)
                {
                    var sourceElement = asArray.GetValue(i);
                    var targetElement = _conversionService.Convert(sourceElement, arrayElementType, targetElementType);
                    list.Add(targetElement);
                }
            }

            return(list);
        }
Ejemplo n.º 14
0
 private static ConstructorInfo DetermineFactoryConstructor(Type targetClass, Type sourceClass)
 {
     return(ConversionUtils.GetConstructorIfAvailable(targetClass, sourceClass));
 }
        public override bool Matches(Type sourceType, Type targetType)
        {
            var targetCheck = ConversionUtils.GetNullableElementType(targetType);

            return(sourceType == typeof(string) && targetCheck == typeof(char));
        }
 public override bool Matches(Type sourceType, Type targetType)
 {
     return(ConversionUtils.CanCreateCompatDictionaryFor(targetType) &&
            CanConvertKey(sourceType, targetType) &&
            CanConvertValue(sourceType, targetType));
 }