Beispiel #1
0
        private void SerializeContainer(IEnumerable e, Type elementType, IDataAdapter data)
        {
            if (settings.PrimitiveAsBase64 && elementType.IsPrimitive)
            {
                int size  = ByteUtils.GetSizeOf(elementType);
                int count = 0;
                // ReSharper disable once UnusedVariable
                foreach (object o in e)
                {
                    count++;
                }

                Action <byte[], int, object> writer = ByteUtils.GetBinaryWriter(elementType);

                byte[] buffer = new byte[size * count];
                int    index  = 0;
                foreach (object o in e)
                {
                    writer(buffer, index, o);
                    index += size;
                }
                data.SetStringValue(Convert.ToBase64String(buffer));
            }
            else
            {
                IDataArray array = data.AddArray();
                foreach (object o in e)
                {
                    SerializeSubValue(elementType, o, array.AddArrayValue(), null);
                }
            }

            haveContainers = true;
        }
Beispiel #2
0
        private void SerializeArrayRank(Array array, Type elementType, int[] coords, int r, IDataAdapter data)
        {
            if (r == coords.Length - 1)
            {
                if (settings.PrimitiveAsBase64 && elementType.IsPrimitive)
                {
                    int size  = ByteUtils.GetSizeOf(elementType);
                    int count = array.GetLength(r);
                    Action <byte[], int, object> writer = ByteUtils.GetBinaryWriter(elementType);

                    byte[] buffer = new byte[size * count];
                    int    index  = 0;
                    for (int i = 0; i < count; i++)
                    {
                        coords[r] = i;
                        writer(buffer, index, array.GetValue(coords));
                        index += size;
                    }
                    data.SetStringValue(Convert.ToBase64String(buffer));
                }
                else
                {
                    IDataArray a = data.AddArray();
                    for (int i = 0; i < array.GetLength(r); i++)
                    {
                        coords[r] = i;
                        SerializeSubValue(elementType, array.GetValue(coords), a.AddArrayValue(), null);
                    }
                }
            }
            else
            {
                IDataArray a = data.AddArray();
                for (int i = 0; i < array.GetLength(r); i++)
                {
                    coords[r] = i;
                    SerializeArrayRank(array, elementType, coords, r + 1, a.AddArrayValue());
                }
            }
        }
Beispiel #3
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;
            }
        }