示例#1
0
            public override void VisitSetItem(IEnumerable setObject, SetDescriptor descriptor, object item, ITypeDescriptor itemDescriptor)
            {
                base.VisitSetItem(setObject, descriptor, item, itemDescriptor);
                var assetReference    = item as AssetReference;
                var attachedReference = AttachedReferenceManager.GetAttachedReference(item);

                if (assetReference != null)
                {
                    AddLink(assetReference,
                            (guid, location) =>
                    {
                        var link = AssetReference.New(guid ?? assetReference.Id, location);
                        descriptor.Add(setObject, link);
                        return(link);
                    });
                }
                else if (attachedReference != null)
                {
                    AddLink(attachedReference,
                            (guid, location) =>
                    {
                        object link = guid.HasValue && guid.Value != AssetId.Empty ? AttachedReferenceManager.CreateProxyObject(descriptor.ElementType, guid.Value, location) : null;
                        descriptor.Add(setObject, link);
                        return(link);
                    });
                }
                else if (item is UFile)
                {
                    AddLink(item,
                            (guid, location) =>
                    {
                        var link = new UFile(location);
                        descriptor.Add(setObject, link);
                        return(link);
                    });
                }
                else if (item is UDirectory)
                {
                    AddLink(item,
                            (guid, location) =>
                    {
                        var link = new UDirectory(location);
                        descriptor.Add(setObject, link);
                        return(link);
                    });
                }
            }
示例#2
0
        /// <summary>
        /// Tries to convert the <paramref name="sourceSet"/> to the type described by <paramref name="setDescriptor"/>.
        /// </summary>
        /// <param name="sourceSet"></param>
        /// <param name="setDescriptor"></param>
        /// <param name="convertedSet"></param>
        /// <returns><c>true</c> if the <paramref name="sourceSet"/> could be converted to the type described by <paramref name="setDescriptor"/>; otherwise, <c>false</c>.</returns>
        private static bool TryConvertSetData([NotNull] object sourceSet, [NotNull] SetDescriptor setDescriptor, out object convertedSet)
        {
            try
            {
                var sourceSetType = sourceSet.GetType();
                // Already same type
                if (setDescriptor.Type == sourceSetType)
                {
                    convertedSet = sourceSet;
                    return(true);
                }

                convertedSet = Activator.CreateInstance(setDescriptor.Type, true);
                var sourceSetDescriptor = (ListDescriptor)TypeDescriptorFactory.Default.Find(sourceSetType);
                foreach (var item in EnumerateList(sourceSet, sourceSetDescriptor))
                {
                    object obj;
                    if (!TypeConverterHelper.TryConvert(item, setDescriptor.ElementType, out obj))
                    {
                        // (optimistic) try to convert the remaining items
                        continue;
                    }
                    setDescriptor.Add(sourceSet, obj);
                }
                return(setDescriptor.GetCollectionCount(convertedSet) > 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
            convertedSet = null;
            return(false);
        }
示例#3
0
        private static bool ConvertForSet(SetDescriptor setDescriptor, ref object data)
        {
            if (SetDescriptor.IsSet(data.GetType()))
            {
                if (!TryConvertSetData(data, setDescriptor, out data))
                {
                    return(false);
                }
            }
            else
            {
                object convertedData;
                if (!TypeConverterHelper.TryConvert(data, setDescriptor.ElementType, out convertedData))
                {
                    return(false);
                }

                var convertedCollection = Activator.CreateInstance(setDescriptor.Type, true);
                setDescriptor.Add(convertedCollection, convertedData);
                data = convertedCollection;
            }
            return(true);
        }