Beispiel #1
0
        private void DeserializeContainer(ref object container, Type type, Type elementType, IDataAdapter data, string addMethodName, bool reverse = false)
        {
            if (container == null)
            {
                container = TypeCache.CreateParameterless(type);
            }

            Action <object, object> addAction = null;

            if (!TypeCache.TryGetTypeAccessor(type, ref addAction))
            {
                addAction = InvocationHelper.CreateSetDelegate(type, elementType, addMethodName);
                TypeCache.AddTypeAccessor(type, addAction);
            }

            if (elementType.IsPrimitive)
            {
                byte[] buffer = Convert.FromBase64String(data.GetStringValue());
                int    size   = ByteUtils.GetSizeOf(elementType);
                int    count  = buffer.Length / size;
                Func <byte[], int, object> reader = ByteUtils.GetBinaryReader(elementType);

                if (reverse)
                {
                    for (int i = count - 1; i >= 0; i--)
                    {
                        addAction(container, reader(buffer, size * i));
                    }
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        addAction(container, reader(buffer, size * i));
                    }
                }

                return;
            }

            IDataArray array = data.GetArray();

            if (reverse)
            {
                foreach (IDataAdapter element in array.GetChildrenReversed())
                {
                    addAction(container, DeserializeObject(elementType, element, null));
                }
            }
            else
            {
                foreach (IDataAdapter element in array.GetChildren())
                {
                    addAction(container, DeserializeObject(elementType, element, null));
                }
            }
        }
Beispiel #2
0
        private void DeserializeContainer(ref object container, Type type, Type elementType, Type returnType, IDataAdapter data, string addMethodName)
        {
            if (container == null)
            {
                container = TypeCache.CreateParameterless(type);
            }

            Func <object, object, object> addFunc = null;

            if (!TypeCache.TryGetTypeAccessor(type, ref addFunc))
            {
                addFunc = InvocationHelper.CreateGetSetDelegate(type, elementType, returnType, addMethodName);
                TypeCache.AddTypeAccessor(type, addFunc);
            }

            foreach (IDataAdapter element in data.GetArray().GetChildren())
            {
                addFunc(container, DeserializeObject(elementType, element, null));
            }
        }
Beispiel #3
0
        private void DeserializeContainer(Type type, TypeCategory category, IDataAdapter data, ref object target)
        {
            // arrays
            if (category == TypeCategory.Array)
            {
                Type elementType = type.GetElementType();
                int  rank        = (int)data.GetIntValue(ATTRIBUTE_ARRAY_RANK, true);
                if (rank == 0)
                {
                    rank = 1;
                }

                int[] lengths = new int[rank];
                ScanArrayRanks(data, elementType, lengths, 0);

                Array array;
                if (target == null)
                {
                    target = array = Array.CreateInstance(elementType, lengths);
                }
                else
                {
                    array = (Array)target;
                }

                int[] coords = new int[array.Rank];
                DeserializeArrayRank(array, elementType, coords, 0, data);

                return;
            }

            // non generic containers
            switch (category)
            {
            case TypeCategory.IList:
                DeserializeContainer(ref target, type, typeof(object), data, "Add");
                return;

            case TypeCategory.Queue:
                DeserializeContainer(ref target, type, typeof(object), data, "Enqueue");
                return;

            case TypeCategory.Stack:
                DeserializeContainer(ref target, type, typeof(object), data, "Push", true);
                return;
            }

            // generics
            Type[] genericArgs = TypeCache.GetTypeGenericArgs(type);

            switch (category)
            {
            case TypeCategory.GenericIList:
                DeserializeContainer(ref target, type, genericArgs[0], data, "Add");
                return;

            case TypeCategory.GenericQueue:
                DeserializeContainer(ref target, type, genericArgs[0], data, "Enqueue");
                return;

            case TypeCategory.GenericStack:
                DeserializeContainer(ref target, type, genericArgs[0], data, "Push", true);
                return;

            case TypeCategory.ISet:
                DeserializeContainer(ref target, type, genericArgs[0], typeof(bool), data, "Add");
                return;

            case TypeCategory.LinkedList:
                DeserializeContainer(ref target, type, genericArgs[0], typeof(LinkedListNode <>).MakeGenericType(genericArgs[0]), data, "AddLast");
                return;
            }

            if (category == TypeCategory.IDictionary)
            {
                if (target == null)
                {
                    target = TypeCache.CreateParameterless(type);
                }

                Action <object, object, object> setAction = null;
                if (!TypeCache.TryGetTypeAccessor(type, ref setAction))
                {
                    setAction = InvocationHelper.CreateSetDelegate(type, genericArgs[0], genericArgs[1], "Add");
                    TypeCache.AddTypeAccessor(type, setAction);
                }

                foreach (IDataAdapter element in data.GetArray().GetChildren())
                {
                    setAction(
                        target,
                        DeserializeObject(genericArgs[0], element.GetChild("Key"), null),
                        DeserializeObject(genericArgs[1], element.GetChild("Value"), null)
                        );
                }
            }
        }
Beispiel #4
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;
            }
        }