예제 #1
0
        /// <summary>
        /// Tries to convert the <paramref name="sourceCollection"/> to the type described by <paramref name="collectionDescriptor"/>.
        /// </summary>
        /// <param name="sourceCollection"></param>
        /// <param name="collectionDescriptor"></param>
        /// <param name="convertedCollection"></param>
        /// <returns><c>true</c> if the <paramref name="sourceCollection"/> could be converted to the type described by <paramref name="collectionDescriptor"/>; otherwise, <c>false</c>.</returns>
        private static bool TryConvertCollectionData([NotNull] object sourceCollection, [NotNull] CollectionDescriptor collectionDescriptor, out object convertedCollection)
        {
            try
            {
                var sourceCollectionType = sourceCollection.GetType();
                // Already same type
                if (collectionDescriptor.Type == sourceCollectionType)
                {
                    convertedCollection = sourceCollection;
                    return(true);
                }

                convertedCollection = Activator.CreateInstance(collectionDescriptor.Type, true);
                var sourceCollectionDescriptor = (CollectionDescriptor)TypeDescriptorFactory.Default.Find(sourceCollectionType);
                foreach (var item in EnumerateCollection(sourceCollection, sourceCollectionDescriptor))
                {
                    object obj;
                    if (!TypeConverterHelper.TryConvert(item, collectionDescriptor.ElementType, out obj))
                    {
                        // (optimistic) try to convert the remaining items
                        continue;
                    }
                    collectionDescriptor.Add(convertedCollection, obj);
                }
                return(collectionDescriptor.GetCollectionCount(convertedCollection) > 0);
            }
            catch (InvalidCastException) { }
            catch (InvalidOperationException) { }
            catch (FormatException) { }
            catch (NotSupportedException) { }
            catch (Exception ex) when(ex.InnerException is InvalidCastException)
            {
            }
            catch (Exception ex) when(ex.InnerException is InvalidOperationException)
            {
            }
            catch (Exception ex) when(ex.InnerException is FormatException)
            {
            }
            catch (Exception ex) when(ex.InnerException is NotSupportedException)
            {
            }

            // Incompatible type and no conversion available
            convertedCollection = null;
            return(false);
        }
예제 #2
0
        private static bool ConvertForCollection(CollectionDescriptor collectionDescriptor, [NotNull] ref object data)
        {
            if (CollectionDescriptor.IsCollection(data.GetType()))
            {
                if (!TryConvertCollectionData(data, collectionDescriptor, out data))
                {
                    return(false);
                }
            }
            else
            {
                object convertedData;
                if (!TypeConverterHelper.TryConvert(data, collectionDescriptor.ElementType, out convertedData))
                {
                    return(false);
                }

                var convertedCollection = Activator.CreateInstance(collectionDescriptor.Type, true);
                collectionDescriptor.Add(convertedCollection, convertedData);
                data = convertedCollection;
            }
            return(true);
        }
예제 #3
0
        /// <summary>
        /// Reads and adds item to the collection.
        /// </summary>
        /// <param name="objectContext">The object context.</param>
        /// <param name="elementType">Type of the element.</param>
        /// <param name="collectionDescriptor">The collection descriptor.</param>
        /// <param name="thisObject">The this object.</param>
        /// <param name="index">The index.</param>
        protected virtual void ReadAddCollectionItem(ref ObjectContext objectContext, Type elementType, CollectionDescriptor collectionDescriptor, object thisObject, int index)
        {
            var value = ReadCollectionItem(ref objectContext, null, elementType, index);

            collectionDescriptor.Add(thisObject, value);
        }