public static bool TryGetValue <T>(this SerializationInfo info, string name, out T value)
 {
     if (info.ContainsEntry(name))
     {
         value = info.GetValue <T>(name);
         return(true);
     }
     value = default(T);
     return(false);
 }
Пример #2
0
        private unsafe MemoryBlock(SerializationInfo info, StreamingContext context)
        {
            this._length = !info.ContainsEntry("length64") ? (long)info.GetInt32("length") : info.GetInt64("length64");

            if (info.ContainsEntry("bitmapWidth"))
            {
                int width  = info.GetInt32("bitmapWidth");
                int height = info.GetInt32("bitmapHeight");
                if ((width != 0 || height != 0) && (long)width * (long)height * 4L != this._length)
                {
                    throw new ApplicationException("Invalid file format: width * height * 4 != length");
                }
            }

            if (info.GetBoolean("hasParent"))
            {
                this._parentBlock = (MemoryBlock)info.GetValue("parentBlock", typeof(MemoryBlock));
                long parentOffset;
                try
                {
                    parentOffset = info.GetInt64("parentOffset64");
                }
                catch (SerializationException ex)
                {
                    parentOffset = (long)info.GetInt32("parentOffset");
                }
                this._valid = true;
            }
            else
            {
                DeferredDeserializer deferredFormatter = context.Context as DeferredDeserializer;
                bool deferred = false;

                info.TryGetValue("deferred", out deferred);

                if (deferred && deferredFormatter != null)
                {
                    // set this object up for deferred deserialization
                    this._buffer = new byte[this._length];
                    deferredFormatter.AddDeferredObject(this);
                }
                else if (deferred && deferredFormatter == null)
                {
                    throw new InvalidOperationException("stream has deferred serialization streams, but a DeferredFormatter was not provided");
                }
                else
                {
                    // not deferred, so load
                    this._buffer = new byte[this._length];
                    this._valid  = true;
                    bool processed = false;
                    if (info.ContainsEntry("pointerData"))
                    {
                        try
                        {
                            byte[] numArray = (byte[])info.GetValue("pointerData", typeof(byte[]));
                            Array.Copy(numArray, 0, _buffer, 0, numArray.LongLength);
                            processed = true;
                        }
                        catch (SerializationException ex)
                        {
                            processed = false;
                        }
                    }
                    if (processed)
                    {
                        return;
                    }

                    uint chunkCount  = info.GetUInt32("chunkCount");
                    int  chunkFormat = info.GetInt32("chunkFormat");
                    if (chunkFormat != 0)
                    {
                        throw new FormatException("Invalid chunkFormat. Expected 0, but got " + chunkFormat.ToString());
                    }
                    long num = 0;
                    for (uint index = 0; index < chunkCount; ++index)
                    {
                        string name     = "chunk" + index.ToString((IFormatProvider)CultureInfo.InvariantCulture);
                        byte[] numArray = info.GetValue <byte[]>(name);

                        fixed(byte *numPtr = numArray)
                        Array.Copy(numArray, 0, _buffer, num, numArray.LongLength);

                        num += numArray.LongLength;
                    }
                    if (num != this._length)
                    {
                        throw new FormatException(string.Format("length={0}, but all the chunks only account for {1} bytes", (object)this._length.ToString("N0"), (object)num.ToString("N0")));
                    }
                }
            }
        }