コード例 #1
0
        public override void ConvertFromData(ConverterContext converterContext, TData data, ref T source)
        {
            if (data == null)
            {
                source = null;
                return;
            }

            if (source == null)
            {
                // Optimize most common cases
                if (typeof(T) == typeof(Dictionary <TKey, TValue>) || typeof(T) == typeof(IDictionary <TKey, TValue>))
                {
                    source = (T)(IDictionary <TKey, TValue>) new Dictionary <TKey, TValue>(data.Count);
                }
                else
                {
                    source = Activator.CreateInstance <T>();
                }
            }
            else
            {
                source.Clear();
            }

            foreach (var item in data)
            {
                var itemData1 = default(TKey);
                var itemData2 = default(TValue);
                converterContext.ConvertFromData(item.Key, ref itemData1);
                converterContext.ConvertFromData(item.Value, ref itemData2);
                source.Add(itemData1, itemData2);
            }
        }
コード例 #2
0
        public override void ConvertFromData(ConverterContext converterContext, TData data, ref T source)
        {
            if (data == null)
            {
                source = null;
                return;
            }

            bool isArray = typeof(T) == typeof(TItem[]);

            if (source == null)
            {
                // Optimize most common cases
                if (typeof(T) == typeof(List <TItem>) || typeof(T) == typeof(IList <TItem>))
                {
                    source = (T)(IList <TItem>) new List <TItem>(data.Count);
                }
                else if (isArray)
                {
                    source = (T)(IList <TItem>) new TItem[data.Count];
                }
                else
                {
                    source = Activator.CreateInstance <T>();
                }
            }
            else if (!isArray)
            {
                source.Clear();
            }

            if (isArray)
            {
                var sourceArray = (TItem[])(object)source;
                for (int i = 0; i < source.Count; ++i)
                {
                    var itemData = default(TItem);
                    converterContext.ConvertFromData(data[i], ref itemData);
                    sourceArray[i] = itemData;
                }
            }
            else
            {
                foreach (var item in data)
                {
                    var itemData = default(TItem);
                    converterContext.ConvertFromData(item, ref itemData);
                    source.Add(itemData);
                }
            }
        }
コード例 #3
0
        public override void ConvertFromData(ConverterContext converterContext, ContentReference <TData> data, ref TSource source)
        {
            var contentSerializerContext = converterContext.Tags.Get(ContentSerializerContext.ContentSerializerContextProperty);

            // TODO: Load through AssetManager if not loaded yet.
            if (contentSerializerContext != null)
            {
                // Not loaded yet?
                var parentAssetReference = contentSerializerContext.AssetReference;
                AssetManager.AssetReference assetReference;
                var value = contentSerializerContext.AssetManager.DeserializeObject(parentAssetReference, out assetReference, data.Location, typeof(TSource), AssetManagerLoaderSettings.IgnoreReferences, converterContext);

                source = converterContext.ConvertFromData <TSource>(value);
            }
            else
            {
                source = converterContext.ConvertFromData <TSource>(data.Value);
            }
        }