Beispiel #1
0
        private object DeserializeArray(long id)
        {
            // Special case for base64 byte arrays
            if (GetComponentType() == typeof(byte[]))
            {
                byte[] data = Convert.FromBase64String(xmlReader.ReadElementString());
                RegisterObject(id, data, null, 0, null, null);
                return(data);
            }

            // Get the array properties
            string strArrayType = xmlReader["arrayType", SoapTypeMapper.SoapEncodingNamespace];

            string[] arrayInfo        = strArrayType.Split(':');
            int      arraySuffixInfo  = arrayInfo[1].LastIndexOf('[');
            String   arrayElementType = arrayInfo[1].Substring(0, arraySuffixInfo);
            String   arraySuffix      = arrayInfo[1].Substring(arraySuffixInfo);

            string[] arrayDims    = arraySuffix.Substring(1, arraySuffix.Length - 2).Trim().Split(',');
            int      numberOfDims = arrayDims.Length;

            int[] lengths = new int[numberOfDims];

            for (int i = 0; i < numberOfDims; i++)
            {
                lengths[i] = Convert.ToInt32(arrayDims[i]);
            }

            int[] indices = new int[numberOfDims];

            // Create the array
            Type  arrayType = mapper.GetType(arrayElementType, xmlReader.LookupNamespace(arrayInfo[0]));
            Array array     = Array.CreateInstance(
                arrayType,
                lengths);

            for (int i = 0; i < numberOfDims; i++)
            {
                indices[i] = array.GetLowerBound(i);
            }

            // Deserialize the array items
            int arrayDepth = xmlReader.Depth;

            xmlReader.Read();
            while (xmlReader.Depth > arrayDepth)
            {
                Type itemType = GetComponentType();
                if (itemType == null)
                {
                    itemType = array.GetType().GetElementType();
                }
                long itemId, itemHref;

                object objItem = DeserializeComponent(itemType,
                                                      out itemId,
                                                      out itemHref,
                                                      id,
                                                      null,
                                                      indices);
                if (itemHref != 0)
                {
                    object obj = objMgr.GetObject(itemHref);
                    if (obj != null)
                    {
                        array.SetValue(obj, indices);
                    }
                    else
                    {
                        RecordFixup(id, itemHref, array, null, null, null, indices);
                    }
                }
                else if (objItem != null && objItem.GetType().IsValueType&& itemId != 0)
                {
                    RecordFixup(id, itemId, array, null, null, null, indices);
                }
                else if (itemId != 0)
                {
                    RegisterObject(itemId, objItem, null, id, null, indices);
                    array.SetValue(objItem, indices);
                }
                else
                {
                    array.SetValue(objItem, indices);
                }

                // Get the next indice
                for (int dim = array.Rank - 1; dim >= 0; dim--)
                {
                    indices[dim]++;
                    if (indices[dim] > array.GetUpperBound(dim))
                    {
                        if (dim > 0)
                        {
                            indices[dim] = array.GetLowerBound(dim);
                            continue;
                        }
                    }
                    break;
                }
            }

            RegisterObject(id, array, null, 0, null, null);
            xmlReader.ReadEndElement();
            return(array);
        }