Esempio n. 1
0
        private void SerializeContainer(object value, IDataAdapter data, TypeCategory category, Type[] genericArgs)
        {
            Type type = value.GetType();

            // arrays
            if (category == TypeCategory.Array)
            {
                Array array       = (Array)value;
                Type  elementType = type.GetElementType();
                int[] coords      = new int[array.Rank];
                if (array.Rank > 1)
                {
                    data.AddIntValue(array.Rank, ATTRIBUTE_ARRAY_RANK, true);
                }
                SerializeArrayRank(array, elementType, coords, 0, data);

                haveContainers = true;
                return;
            }

            // dictionaries
            if (category == TypeCategory.IDictionary)
            {
                Type keyType   = genericArgs[0];
                Type valueType = genericArgs[1];

                Func <object, object> getKeyFunc   = null;
                Func <object, object> getValueFunc = null;

                IDataArray array = data.AddArray();

                foreach (object o in (IEnumerable)value)
                {
                    if (getKeyFunc == null)
                    {
                        Type objectType = o.GetType();
                        if (!TypeCache.TryGetNamedAccessor(objectType, "Key", ref getKeyFunc))
                        {
                            getKeyFunc = InvocationHelper.CreateGetDelegate(o.GetType(), keyType, objectType.GetProperty("Key").GetMethod);
                            TypeCache.AddTypeNamedAccessor(objectType, "Key", getKeyFunc);
                        }
                        if (!TypeCache.TryGetNamedAccessor(objectType, "Value", ref getValueFunc))
                        {
                            getValueFunc = InvocationHelper.CreateGetDelegate(o.GetType(), valueType, objectType.GetProperty("Value").GetMethod);
                            TypeCache.AddTypeNamedAccessor(objectType, "Value", getValueFunc);
                        }
                    }


                    IDataAdapter itemEl = array.AddArrayValue();
                    SerializeSubValue(keyType, getKeyFunc(o), itemEl.AddChild(settings.DictionaryKeyName), null);
                    SerializeSubValue(valueType, getValueFunc(o), itemEl.AddChild(settings.DictionaryValueName), null);
                }
                haveContainers = true;

                return;
            }

            // generics
            if (category == TypeCategory.GenericIList ||
                category == TypeCategory.ISet ||
                category == TypeCategory.GenericQueue ||
                category == TypeCategory.GenericStack ||
                category == TypeCategory.LinkedList)
            {
                SerializeContainer((IEnumerable)value, genericArgs[0], data);
                return;
            }

            // non-generic versions
            if (category == TypeCategory.IList ||
                category == TypeCategory.Queue ||
                category == TypeCategory.Stack)
            {
                SerializeContainer((IEnumerable)value, typeof(object), data);
                return;
            }
        }