/// <summary>
        /// This type supports the Fluorine infrastructure and is not intended to be used directly from your code.
        /// </summary>
        /// <param name="context">An ITypeDescriptorContext that provides a format context.</param>
        /// <param name="culture">A CultureInfo object. If a null reference (Nothing in Visual Basic) is passed, the current culture is assumed.</param>
        /// <param name="value">The Object to convert.</param>
        /// <param name="destinationType">The Type to convert the value parameter to.</param>
        /// <returns>An Object that represents the converted value.</returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            ArrayCollection ac = value as ArrayCollection;

            ValidationUtils.ArgumentNotNull(ac, "value");
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }

            if (destinationType == typeof(ArrayCollection))
            {
                return(value);
            }
            if (destinationType.IsArray)
            {
                return(ac.ToArray());
            }
#if !SILVERLIGHT
            if (destinationType == typeof(ArrayList))
            {
                if (ac.List is ArrayList)
                {
                    return(ac.List);
                }
                return(ArrayList.Adapter(ac.List));
            }
#endif
            if (destinationType == typeof(IList))
            {
                return(ac.List);
            }
            //generic interface
            Type typeGenericICollection = destinationType.GetInterface("System.Collections.Generic.ICollection`1", false);
            if (typeGenericICollection != null)
            {
                object     obj             = TypeHelper.CreateInstance(destinationType);
                MethodInfo miAddCollection = typeGenericICollection.GetMethod("Add");
                if (miAddCollection != null)
                {
                    ParameterInfo[] parameters = miAddCollection.GetParameters();
                    if (parameters != null && parameters.Length == 1)
                    {
                        Type  parameterType = parameters[0].ParameterType;
                        IList list          = (IList)value;
                        for (int i = 0; i < list.Count; i++)
                        {
                            miAddCollection.Invoke(obj, new object[] { TypeHelper.ChangeType(list[i], parameterType) });
                        }
                        return(obj);
                    }
                }
            }
            Type typeIList = destinationType.GetInterface("System.Collections.IList", false);
            if (typeIList != null)
            {
                object obj  = TypeHelper.CreateInstance(destinationType);
                IList  list = obj as IList;
                for (int i = 0; i < ac.List.Count; i++)
                {
                    list.Add(ac.List[i]);
                }
                return(obj);
            }
#if !SILVERLIGHT
            return(base.ConvertTo(context, culture, value, destinationType));
#else
            return(base.ConvertTo(value, destinationType));
#endif
        }
Beispiel #2
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            object          obj2;
            IList           list;
            int             num;
            ArrayCollection arrays = value as ArrayCollection;

            ValidationUtils.ArgumentNotNull(arrays, "value");
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if (destinationType == typeof(ArrayCollection))
            {
                return(value);
            }
            if (destinationType.IsArray)
            {
                return(arrays.ToArray());
            }
            if (destinationType == typeof(ArrayList))
            {
                if (arrays.List is ArrayList)
                {
                    return(arrays.List);
                }
                return(ArrayList.Adapter(arrays.List));
            }
            if (destinationType == typeof(IList))
            {
                return(arrays.List);
            }
            if (destinationType.GetInterface("System.Collections.Generic.ICollection`1", false) != null)
            {
                obj2 = TypeHelper.CreateInstance(destinationType);
                MethodInfo method = destinationType.GetMethod("Add");
                if (method != null)
                {
                    ParameterInfo[] parameters = method.GetParameters();
                    if ((parameters != null) && (parameters.Length == 1))
                    {
                        Type parameterType = parameters[0].ParameterType;
                        list = (IList)value;
                        for (num = 0; num < list.Count; num++)
                        {
                            method.Invoke(obj2, new object[] { TypeHelper.ChangeType(list[num], parameterType) });
                        }
                        return(obj2);
                    }
                }
            }
            if (destinationType.GetInterface("System.Collections.IList", false) != null)
            {
                obj2 = TypeHelper.CreateInstance(destinationType);
                list = obj2 as IList;
                for (num = 0; num < arrays.List.Count; num++)
                {
                    list.Add(arrays.List[num]);
                }
                return(obj2);
            }
            return(base.ConvertTo(context, culture, value, destinationType));
        }