Example #1
0
            public override void VisitDictionaryKeyValue(object dictionaryObj, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
            {
                base.VisitDictionaryKeyValue(dictionaryObj, descriptor, key, keyDescriptor, value, valueDescriptor);
                var assetReference    = value as AssetReference;
                var assetBase         = value as AssetBase;
                var attachedReference = value != null?AttachedReferenceManager.GetAttachedReference(value) : null;

                if (assetReference != null)
                {
                    AddLink(assetReference,
                            (guid, location) =>
                    {
                        var newValue = AssetReference.New(descriptor.ValueType, guid.HasValue ? guid.Value : assetReference.Id, location);
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
                else if (assetBase != null)
                {
                    AddLink(assetBase,
                            (guid, location) =>
                    {
                        var newValue = new AssetBase(location, assetBase.Asset);
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
                else if (attachedReference != null)
                {
                    AddLink(new AttachedContentReference(attachedReference),
                            (guid, location) =>
                    {
                        object newValue = guid.HasValue && guid.Value != Guid.Empty ? AttachedReferenceManager.CreateSerializableVersion(descriptor.ValueType, guid.Value, location) : null;
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
                else if (value is UFile)
                {
                    AddLink(value,
                            (guid, location) =>
                    {
                        var newValue = new UFile(location);
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
                else if (value is UDirectory)
                {
                    AddLink(value,
                            (guid, location) =>
                    {
                        var newValue = new UDirectory(location);
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
            }
            public override void VisitDictionaryKeyValue(object dictionaryObj, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
            {
                base.VisitDictionaryKeyValue(dictionaryObj, descriptor, key, keyDescriptor, value, valueDescriptor);
                var assetReference = value as AssetReference;

                if (assetReference != null)
                {
                    AddLink(assetReference,
                            (guid, location) =>
                    {
                        var newValue = AssetReference.New(descriptor.ValueType, guid.HasValue ? guid.Value : assetReference.Id, location);
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
                else
                {
                    var assetBase = value as AssetBase;
                    if (assetBase != null)
                    {
                        AddLink(assetBase,
                                (guid, location) =>
                        {
                            var newValue = new AssetBase(location, assetBase.Asset);
                            descriptor.SetValue(dictionaryObj, key, newValue);
                            return(newValue);
                        });
                    }
                    else if (value is UFile)
                    {
                        AddLink(value,
                                (guid, location) =>
                        {
                            var newValue = new UFile(location);
                            descriptor.SetValue(dictionaryObj, key, newValue);
                            return(newValue);
                        });
                    }
                    else if (value is UDirectory)
                    {
                        AddLink(value,
                                (guid, location) =>
                        {
                            var newValue = new UDirectory(location);
                            descriptor.SetValue(dictionaryObj, key, newValue);
                            return(newValue);
                        });
                    }
                }
            }
Example #3
0
        private static bool ConvertForDictionary(DictionaryDescriptor dictionaryDescriptor, ref object data)
        {
            object convertedDictionary;

            if (DictionaryDescriptor.IsDictionary(data.GetType()))
            {
                if (!TryConvertDictionaryData(data, dictionaryDescriptor, out convertedDictionary))
                {
                    return(false);
                }
            }
            else
            {
                var dataType = data.GetType();
                var key      = dataType.GetMember("Key").OfType <PropertyInfo>().FirstOrDefault()?.GetValue(data);
                if (key == null || !TypeConverterHelper.TryConvert(key, dictionaryDescriptor.KeyType, out key))
                {
                    return(false);
                }

                var value = dataType.GetMember("Value").OfType <PropertyInfo>().FirstOrDefault()?.GetValue(data);
                if (value == null || !TypeConverterHelper.TryConvert(value, dictionaryDescriptor.ValueType, out value))
                {
                    return(false);
                }

                convertedDictionary = Activator.CreateInstance(dictionaryDescriptor.Type, true);
                dictionaryDescriptor.SetValue(convertedDictionary, key, value);
            }
            data = convertedDictionary;
            return(true);
        }
Example #4
0
            public override void VisitDictionaryKeyValue(object dictionaryObj, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
            {
                base.VisitDictionaryKeyValue(dictionaryObj, descriptor, key, keyDescriptor, value, valueDescriptor);
                var assetReference    = value as AssetReference;
                var attachedReference = AttachedReferenceManager.GetAttachedReference(value);

                if (assetReference != null)
                {
                    AddLink(assetReference,
                            (guid, location) =>
                    {
                        var newValue = AssetReference.New(guid ?? assetReference.Id, location);
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
                else if (attachedReference != null)
                {
                    AddLink(attachedReference,
                            (guid, location) =>
                    {
                        object newValue = guid.HasValue && guid.Value != AssetId.Empty ? AttachedReferenceManager.CreateProxyObject(descriptor.ValueType, guid.Value, location) : null;
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
                else if (value is UFile)
                {
                    AddLink(value,
                            (guid, location) =>
                    {
                        var newValue = new UFile(location);
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
                else if (value is UDirectory)
                {
                    AddLink(value,
                            (guid, location) =>
                    {
                        var newValue = new UDirectory(location);
                        descriptor.SetValue(dictionaryObj, key, newValue);
                        return(newValue);
                    });
                }
            }
Example #5
0
        /// <summary>
        /// Tries to convert the <paramref name="sourceDictionary"/> to the type described by <paramref name="dictionaryDescriptor"/>.
        /// </summary>
        /// <param name="sourceDictionary"></param>
        /// <param name="dictionaryDescriptor"></param>
        /// <param name="convertedDictionary"></param>
        /// <returns><c>true</c> if the <paramref name="sourceDictionary"/> could be converted to the type described by <paramref name="dictionaryDescriptor"/>; otherwise, <c>false</c>.</returns>
        private static bool TryConvertDictionaryData([NotNull] object sourceDictionary, [NotNull] DictionaryDescriptor dictionaryDescriptor, out object convertedDictionary)
        {
            try
            {
                var sourceDictionaryType = sourceDictionary.GetType();
                // Already same type
                if (dictionaryDescriptor.Type == sourceDictionary.GetType())
                {
                    convertedDictionary = sourceDictionary;
                    return(true);
                }

                convertedDictionary = Activator.CreateInstance(dictionaryDescriptor.Type, true);
                var sourceDictionaryDescriptor = (DictionaryDescriptor)TypeDescriptorFactory.Default.Find(sourceDictionaryType);
                foreach (var k in sourceDictionaryDescriptor.GetKeys(sourceDictionary))
                {
                    var key = k;
                    if (!TypeConverterHelper.TryConvert(key, dictionaryDescriptor.KeyType, out key))
                    {
                        // (optimistic) try to convert the remaining items
                        continue;
                    }
                    var value = sourceDictionaryDescriptor.GetValue(sourceDictionary, k);
                    if (!TypeConverterHelper.TryConvert(value, dictionaryDescriptor.ValueType, out value))
                    {
                        // (optimistic) try to convert the remaining items
                        continue;
                    }
                    dictionaryDescriptor.SetValue(convertedDictionary, key, value);
                }
                return(dictionaryDescriptor.GetKeys(convertedDictionary)?.Count > 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
            convertedDictionary = null;
            return(false);
        }
 public override void VisitDictionaryKeyValue(object dictionaryObj, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
 {
     base.VisitDictionaryKeyValue(dictionaryObj, descriptor, key, keyDescriptor, value, valueDescriptor);
     var assetReference = value as AssetReference;
     var assetBase = value as AssetBase;
     var attachedReference = value != null ? AttachedReferenceManager.GetAttachedReference(value) : null;
     if (assetReference != null)
     {
         AddLink(assetReference,
             (guid, location) =>
             {
                 var newValue = AssetReference.New(descriptor.ValueType, guid.HasValue ? guid.Value : assetReference.Id, location);
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
     else if (assetBase != null)
     {
         AddLink(assetBase,
             (guid, location) =>
             {
                 var newValue = new AssetBase(location, assetBase.Asset);
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
     else if (attachedReference != null)
     {
         AddLink(attachedReference,
             (guid, location) =>
             {
                 object newValue = guid.HasValue && guid.Value != Guid.Empty ? AttachedReferenceManager.CreateSerializableVersion(descriptor.ValueType, guid.Value, location) : null;
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
     else if (value is UFile)
     {
         AddLink(value,
             (guid, location) =>
             {
                 var newValue = new UFile(location);
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
     else if (value is UDirectory)
     {
         AddLink(value,
             (guid, location) =>
             {
                 var newValue = new UDirectory(location);
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
 }