Exemplo n.º 1
0
        public override void ConvertToData(ConverterContext converterContext, ref TData data, T obj)
        {
            if (obj == null)
            {
                data = null;
                return;
            }

            if (data == null)
            {
                // Optimize most common cases
                if (typeof(TData) == typeof(Dictionary <TDataKey, TDataValue>) || typeof(TData) == typeof(IDictionary <TDataKey, TDataValue>))
                {
                    data = (TData)(IDictionary <TDataKey, TDataValue>) new Dictionary <TDataKey, TDataValue>(obj.Count);
                }
                else
                {
                    data = Activator.CreateInstance <TData>();
                }
            }
            else
            {
                data.Clear();
            }

            foreach (var item in obj)
            {
                var itemData1 = default(TDataKey);
                var itemData2 = default(TDataValue);
                converterContext.ConvertToData(ref itemData1, item.Key);
                converterContext.ConvertToData(ref itemData2, item.Value);
                data.Add(itemData1, itemData2);
            }
        }
Exemplo n.º 2
0
        public override void ConvertToData(ConverterContext converterContext, ref ContentReference <TData> data, TSource obj)
        {
            var contentSerializerContext = converterContext.Tags.Get(ContentSerializerContext.ContentSerializerContextProperty);

            // TODO: When to stop conversion and switch to Location?
            if (contentSerializerContext != null)
            {
                data = new ContentReference <TData>()
                {
                    Value = converterContext.ConvertToData <TData>(obj)
                };
            }
            else
            {
                data = new ContentReference <TData>()
                {
                    Value = converterContext.ConvertToData <TData>(obj)
                };
            }
        }
Exemplo n.º 3
0
        public override void ConvertToData(ConverterContext converterContext, ref TData data, T obj)
        {
            if (obj == null)
            {
                data = null;
                return;
            }

            bool isArray = typeof(TData) == typeof(TDataItem[]);

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

            int index = 0;

            foreach (var item in obj)
            {
                var itemData = default(TDataItem);
                converterContext.ConvertToData(ref itemData, item);
                if (isArray)
                {
                    data[index] = itemData;
                }
                else
                {
                    data.Add(itemData);
                }
                index++;
            }
        }