/// <include file='doc\ActiveXMessageFormatter.uex' path='docs/doc[@for="ActiveXMessageFormatter.Write"]/*' />
        /// <devdoc>
        ///    This method is used to write the given object into the given message.
        ///     If the formatter cannot understand the given object, an exception is thrown.
        /// </devdoc>
        public void Write(Message message, object obj)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            Stream stream;
            int    variantType;

            if (obj is string)
            {
                int size = ((string)obj).Length * 2;
                if (this.internalBuffer == null || this.internalBuffer.Length < size)
                {
                    this.internalBuffer = new byte[size];
                }

                if (unicodeEncoding == null)
                {
                    this.unicodeEncoding = new UnicodeEncoding();
                }

                this.unicodeEncoding.GetBytes(((string)obj).ToCharArray(), 0, size / 2, this.internalBuffer, 0);
                message.properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, this.internalBuffer);
                message.properties.AdjustSize(NativeMethods.MESSAGE_PROPID_BODY, size);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE, size);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE, VT_LPWSTR);
                return;
            }
            else if (obj is byte[])
            {
                byte[] bytes = (byte[])obj;
                if (this.internalBuffer == null || this.internalBuffer.Length < bytes.Length)
                {
                    this.internalBuffer = new byte[bytes.Length];
                }

                Array.Copy(bytes, this.internalBuffer, bytes.Length);
                message.properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, this.internalBuffer);
                message.properties.AdjustSize(NativeMethods.MESSAGE_PROPID_BODY, bytes.Length);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE, bytes.Length);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE, VT_UI1 | VT_VECTOR);
                return;
            }
            else if (obj is char[])
            {
                char[] chars = (char[])obj;
                int    size  = chars.Length * 2;
                if (this.internalBuffer == null || this.internalBuffer.Length < size)
                {
                    this.internalBuffer = new byte[size];
                }

                if (unicodeEncoding == null)
                {
                    this.unicodeEncoding = new UnicodeEncoding();
                }

                this.unicodeEncoding.GetBytes(chars, 0, size / 2, this.internalBuffer, 0);
                message.properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, this.internalBuffer);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE, size);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE, VT_LPWSTR);
                return;
            }
            else if (obj is byte)
            {
                stream = new MemoryStream(1);
                stream.Write(new byte[] { (byte)obj }, 0, 1);
                variantType = VT_UI1;
            }
            else if (obj is bool)
            {
                stream = new MemoryStream(1);
                if ((bool)obj)
                {
                    stream.Write(new byte[] { 0xff }, 0, 1);
                }
                else
                {
                    stream.Write(new byte[] { 0x00 }, 0, 1);
                }
                variantType = VT_BOOL;
            }
            else if (obj is char)
            {
                stream = new MemoryStream(2);
                byte[] bytes = BitConverter.GetBytes((Char)obj);
                stream.Write(bytes, 0, 2);
                variantType = VT_UI2;
            }
            else if (obj is Decimal)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes(Decimal.ToOACurrency((Decimal)obj));
                stream.Write(bytes, 0, 8);
                variantType = VT_CY;
            }
            else if (obj is DateTime)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes(((DateTime)obj).Ticks);
                stream.Write(bytes, 0, 8);
                variantType = VT_DATE;
            }
            else if (obj is Double)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes((Double)obj);
                stream.Write(bytes, 0, 8);
                variantType = VT_R8;
            }
            else if (obj is Int16)
            {
                stream = new MemoryStream(2);
                byte[] bytes = BitConverter.GetBytes((short)obj);
                stream.Write(bytes, 0, 2);
                variantType = VT_I2;
            }
            else if (obj is UInt16)
            {
                stream = new MemoryStream(2);
                byte[] bytes = BitConverter.GetBytes((UInt16)obj);
                stream.Write(bytes, 0, 2);
                variantType = VT_UI2;
            }
            else if (obj is Int32)
            {
                stream = new MemoryStream(4);
                byte[] bytes = BitConverter.GetBytes((int)obj);
                stream.Write(bytes, 0, 4);
                variantType = VT_I4;
            }
            else if (obj is UInt32)
            {
                stream = new MemoryStream(4);
                byte[] bytes = BitConverter.GetBytes((UInt32)obj);
                stream.Write(bytes, 0, 4);
                variantType = VT_UI4;
            }
            else if (obj is Int64)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes((Int64)obj);
                stream.Write(bytes, 0, 8);
                variantType = VT_I8;
            }
            else if (obj is UInt64)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes((UInt64)obj);
                stream.Write(bytes, 0, 8);
                variantType = VT_UI8;
            }
            else if (obj is Single)
            {
                stream = new MemoryStream(4);
                byte[] bytes = BitConverter.GetBytes((float)obj);
                stream.Write(bytes, 0, 4);
                variantType = VT_R4;
            }
            else if (obj is IPersistStream)
            {
                IPersistStream          pstream   = (IPersistStream)obj;
                ComStreamFromDataStream comStream = new ComStreamFromDataStream(new MemoryStream());
                NativeMethods.OleSaveToStream(pstream, comStream);
                stream      = comStream.GetDataStream();
                variantType = VT_STREAMED_OBJECT;
            }
            else if (obj == null)
            {
                stream      = new MemoryStream();
                variantType = VT_NULL;
            }
            else
            {
                throw new InvalidOperationException(Res.GetString(Res.InvalidTypeSerialization));
            }

            message.BodyStream = stream;
            message.BodyType   = variantType;
        }
        /// <include file='doc\ActiveXMessageFormatter.uex' path='docs/doc[@for="ActiveXMessageFormatter.Write"]/*' />
        /// <devdoc>
        ///    This method is used to write the given object into the given message.  
        ///     If the formatter cannot understand the given object, an exception is thrown.
        /// </devdoc>
        public void Write(Message message, object obj)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            Stream stream;
            int variantType;
            if (obj is string)
            {
                int size = ((string)obj).Length * 2;
                if (this.internalBuffer == null || this.internalBuffer.Length < size)
                    this.internalBuffer = new byte[size];

                if (unicodeEncoding == null)
                    this.unicodeEncoding = new UnicodeEncoding();

                this.unicodeEncoding.GetBytes(((string)obj).ToCharArray(), 0, size / 2, this.internalBuffer, 0);
                message.properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, this.internalBuffer);
                message.properties.AdjustSize(NativeMethods.MESSAGE_PROPID_BODY, size);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE, size);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE, VT_LPWSTR);
                return;
            }
            else if (obj is byte[])
            {
                byte[] bytes = (byte[])obj;
                if (this.internalBuffer == null || this.internalBuffer.Length < bytes.Length)
                    this.internalBuffer = new byte[bytes.Length];

                Array.Copy(bytes, this.internalBuffer, bytes.Length);
                message.properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, this.internalBuffer);
                message.properties.AdjustSize(NativeMethods.MESSAGE_PROPID_BODY, bytes.Length);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE, bytes.Length);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE, VT_UI1 | VT_VECTOR);
                return;
            }
            else if (obj is char[])
            {
                char[] chars = (char[])obj;
                int size = chars.Length * 2;
                if (this.internalBuffer == null || this.internalBuffer.Length < size)
                    this.internalBuffer = new byte[size];

                if (unicodeEncoding == null)
                    this.unicodeEncoding = new UnicodeEncoding();

                this.unicodeEncoding.GetBytes(chars, 0, size / 2, this.internalBuffer, 0);
                message.properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, this.internalBuffer);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE, size);
                message.properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE, VT_LPWSTR);
                return;
            }
            else if (obj is byte)
            {
                stream = new MemoryStream(1);
                stream.Write(new byte[] { (byte)obj }, 0, 1);
                variantType = VT_UI1;
            }
            else if (obj is bool)
            {
                stream = new MemoryStream(1);
                if ((bool)obj)
                    stream.Write(new byte[] { 0xff }, 0, 1);
                else
                    stream.Write(new byte[] { 0x00 }, 0, 1);
                variantType = VT_BOOL;
            }
            else if (obj is char)
            {
                stream = new MemoryStream(2);
                byte[] bytes = BitConverter.GetBytes((Char)obj);
                stream.Write(bytes, 0, 2);
                variantType = VT_UI2;
            }
            else if (obj is Decimal)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes(Decimal.ToOACurrency((Decimal)obj));
                stream.Write(bytes, 0, 8);
                variantType = VT_CY;
            }
            else if (obj is DateTime)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes(((DateTime)obj).Ticks);
                stream.Write(bytes, 0, 8);
                variantType = VT_DATE;
            }
            else if (obj is Double)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes((Double)obj);
                stream.Write(bytes, 0, 8);
                variantType = VT_R8;
            }
            else if (obj is Int16)
            {
                stream = new MemoryStream(2);
                byte[] bytes = BitConverter.GetBytes((short)obj);
                stream.Write(bytes, 0, 2);
                variantType = VT_I2;
            }
            else if (obj is UInt16)
            {
                stream = new MemoryStream(2);
                byte[] bytes = BitConverter.GetBytes((UInt16)obj);
                stream.Write(bytes, 0, 2);
                variantType = VT_UI2;
            }
            else if (obj is Int32)
            {
                stream = new MemoryStream(4);
                byte[] bytes = BitConverter.GetBytes((int)obj);
                stream.Write(bytes, 0, 4);
                variantType = VT_I4;
            }
            else if (obj is UInt32)
            {
                stream = new MemoryStream(4);
                byte[] bytes = BitConverter.GetBytes((UInt32)obj);
                stream.Write(bytes, 0, 4);
                variantType = VT_UI4;
            }
            else if (obj is Int64)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes((Int64)obj);
                stream.Write(bytes, 0, 8);
                variantType = VT_I8;
            }
            else if (obj is UInt64)
            {
                stream = new MemoryStream(8);
                byte[] bytes = BitConverter.GetBytes((UInt64)obj);
                stream.Write(bytes, 0, 8);
                variantType = VT_UI8;
            }
            else if (obj is Single)
            {
                stream = new MemoryStream(4);
                byte[] bytes = BitConverter.GetBytes((float)obj);
                stream.Write(bytes, 0, 4);
                variantType = VT_R4;
            }
            else if (obj is IPersistStream)
            {
                IPersistStream pstream = (IPersistStream)obj;
                ComStreamFromDataStream comStream = new ComStreamFromDataStream(new MemoryStream());
                NativeMethods.OleSaveToStream(pstream, comStream);
                stream = comStream.GetDataStream();
                variantType = VT_STREAMED_OBJECT;
            }
            else if (obj == null)
            {
                stream = new MemoryStream();
                variantType = VT_NULL;
            }
            else
            {
                throw new InvalidOperationException(Res.GetString(Res.InvalidTypeSerialization));
            }

            message.BodyStream = stream;
            message.BodyType = variantType;
        }
        /// <include file='doc\ActiveXMessageFormatter.uex' path='docs/doc[@for="ActiveXMessageFormatter.Read"]/*' />
        /// <devdoc>
        ///    This method is used to read the contents from the given message
        ///     and create an object.
        /// </devdoc>
        public object Read(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            Stream stream;

            byte[] bytes;
            byte[] newBytes;
            int    size;
            int    variantType = message.BodyType;

            switch (variantType)
            {
            case VT_LPSTR:
                bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                size  = message.properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE);

                if (this.internalCharBuffer == null || this.internalCharBuffer.Length < size)
                {
                    this.internalCharBuffer = new char[size];
                }

                if (asciiEncoding == null)
                {
                    this.asciiEncoding = new ASCIIEncoding();
                }

                this.asciiEncoding.GetChars(bytes, 0, size, this.internalCharBuffer, 0);
                return(new String(this.internalCharBuffer, 0, size));

            case VT_BSTR:
            case VT_LPWSTR:
                bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                size  = message.properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE) / 2;

                if (this.internalCharBuffer == null || this.internalCharBuffer.Length < size)
                {
                    this.internalCharBuffer = new char[size];
                }

                if (unicodeEncoding == null)
                {
                    this.unicodeEncoding = new UnicodeEncoding();
                }

                this.unicodeEncoding.GetChars(bytes, 0, size * 2, this.internalCharBuffer, 0);
                return(new String(this.internalCharBuffer, 0, size));

            case VT_VECTOR | VT_UI1:
                bytes    = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                size     = message.properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE);
                newBytes = new byte[size];
                Array.Copy(bytes, newBytes, size);

                return(newBytes);

            case VT_BOOL:
                bytes    = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                newBytes = new byte[1];
                Array.Copy(bytes, newBytes, 1);
                if (bytes[0] != 0)
                {
                    return(true);
                }

                return(false);

            case VT_CLSID:
                bytes    = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                newBytes = new byte[16];
                Array.Copy(bytes, newBytes, 16);
                return(new Guid(newBytes));

            case VT_CY:
                bytes    = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                newBytes = new byte[8];
                Array.Copy(bytes, newBytes, 8);
                return(Decimal.FromOACurrency(BitConverter.ToInt64(newBytes, 0)));

            case VT_DATE:
                bytes    = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                newBytes = new byte[8];
                Array.Copy(bytes, newBytes, 8);
                return(new DateTime(BitConverter.ToInt64(newBytes, 0)));

            case VT_I1:
            case VT_UI1:
                stream = message.BodyStream;
                bytes  = new byte[1];
                stream.Read(bytes, 0, 1);
                return(bytes[0]);

            case VT_I2:
                stream = message.BodyStream;
                bytes  = new byte[2];
                stream.Read(bytes, 0, 2);
                return(BitConverter.ToInt16(bytes, 0));

            case VT_UI2:
                stream = message.BodyStream;
                bytes  = new byte[2];
                stream.Read(bytes, 0, 2);
                return(BitConverter.ToUInt16(bytes, 0));

            case VT_I4:
                stream = message.BodyStream;
                bytes  = new byte[4];
                stream.Read(bytes, 0, 4);
                return(BitConverter.ToInt32(bytes, 0));

            case VT_UI4:
                stream = message.BodyStream;
                bytes  = new byte[4];
                stream.Read(bytes, 0, 4);
                return(BitConverter.ToUInt32(bytes, 0));

            case VT_I8:
                stream = message.BodyStream;
                bytes  = new byte[8];
                stream.Read(bytes, 0, 8);
                return(BitConverter.ToInt64(bytes, 0));

            case VT_UI8:
                stream = message.BodyStream;
                bytes  = new byte[8];
                stream.Read(bytes, 0, 8);
                return(BitConverter.ToUInt64(bytes, 0));

            case VT_R4:
                stream = message.BodyStream;
                bytes  = new byte[4];
                stream.Read(bytes, 0, 4);
                return(BitConverter.ToSingle(bytes, 0));

            case VT_R8:
                stream = message.BodyStream;
                bytes  = new byte[8];
                stream.Read(bytes, 0, 8);
                return(BitConverter.ToDouble(bytes, 0));

            case VT_NULL:
                return(null);

            case VT_STREAMED_OBJECT:
                stream = message.BodyStream;
                ComStreamFromDataStream comStream = new ComStreamFromDataStream(stream);
                return(NativeMethods.OleLoadFromStream(comStream, ref NativeMethods.IID_IUnknown));

            case VT_STORED_OBJECT:
                throw new NotSupportedException(Res.GetString(Res.StoredObjectsNotSupported));

            default:
                throw new InvalidOperationException(Res.GetString(Res.InvalidTypeDeserialization));
            }
        }
        /// <include file='doc\ActiveXMessageFormatter.uex' path='docs/doc[@for="ActiveXMessageFormatter.Read"]/*' />
        /// <devdoc>
        ///    This method is used to read the contents from the given message 
        ///     and create an object.
        /// </devdoc>
        public object Read(Message message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            Stream stream;
            byte[] bytes;
            byte[] newBytes;
            int size;
            int variantType = message.BodyType;
            switch (variantType)
            {
                case VT_LPSTR:
                    bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                    size = message.properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE);

                    if (this.internalCharBuffer == null || this.internalCharBuffer.Length < size)
                        this.internalCharBuffer = new char[size];

                    if (asciiEncoding == null)
                        this.asciiEncoding = new ASCIIEncoding();

                    this.asciiEncoding.GetChars(bytes, 0, size, this.internalCharBuffer, 0);
                    return new String(this.internalCharBuffer, 0, size);
                case VT_BSTR:
                case VT_LPWSTR:
                    bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                    size = message.properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE) / 2;

                    if (this.internalCharBuffer == null || this.internalCharBuffer.Length < size)
                        this.internalCharBuffer = new char[size];

                    if (unicodeEncoding == null)
                        this.unicodeEncoding = new UnicodeEncoding();

                    this.unicodeEncoding.GetChars(bytes, 0, size * 2, this.internalCharBuffer, 0);
                    return new String(this.internalCharBuffer, 0, size);
                case VT_VECTOR | VT_UI1:
                    bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                    size = message.properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE);
                    newBytes = new byte[size];
                    Array.Copy(bytes, newBytes, size);

                    return newBytes;
                case VT_BOOL:
                    bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                    newBytes = new byte[1];
                    Array.Copy(bytes, newBytes, 1);
                    if (bytes[0] != 0)
                        return true;

                    return false;
                case VT_CLSID:
                    bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                    newBytes = new byte[16];
                    Array.Copy(bytes, newBytes, 16);
                    return new Guid(newBytes);
                case VT_CY:
                    bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                    newBytes = new byte[8];
                    Array.Copy(bytes, newBytes, 8);
                    return Decimal.FromOACurrency(BitConverter.ToInt64(newBytes, 0));
                case VT_DATE:
                    bytes = message.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
                    newBytes = new byte[8];
                    Array.Copy(bytes, newBytes, 8);
                    return new DateTime(BitConverter.ToInt64(newBytes, 0));
                case VT_I1:
                case VT_UI1:
                    stream = message.BodyStream;
                    bytes = new byte[1];
                    stream.Read(bytes, 0, 1);
                    return bytes[0];
                case VT_I2:
                    stream = message.BodyStream;
                    bytes = new byte[2];
                    stream.Read(bytes, 0, 2);
                    return BitConverter.ToInt16(bytes, 0);
                case VT_UI2:
                    stream = message.BodyStream;
                    bytes = new byte[2];
                    stream.Read(bytes, 0, 2);
                    return BitConverter.ToUInt16(bytes, 0);
                case VT_I4:
                    stream = message.BodyStream;
                    bytes = new byte[4];
                    stream.Read(bytes, 0, 4);
                    return BitConverter.ToInt32(bytes, 0);
                case VT_UI4:
                    stream = message.BodyStream;
                    bytes = new byte[4];
                    stream.Read(bytes, 0, 4);
                    return BitConverter.ToUInt32(bytes, 0);
                case VT_I8:
                    stream = message.BodyStream;
                    bytes = new byte[8];
                    stream.Read(bytes, 0, 8);
                    return BitConverter.ToInt64(bytes, 0);
                case VT_UI8:
                    stream = message.BodyStream;
                    bytes = new byte[8];
                    stream.Read(bytes, 0, 8);
                    return BitConverter.ToUInt64(bytes, 0);
                case VT_R4:
                    stream = message.BodyStream;
                    bytes = new byte[4];
                    stream.Read(bytes, 0, 4);
                    return BitConverter.ToSingle(bytes, 0);
                case VT_R8:
                    stream = message.BodyStream;
                    bytes = new byte[8];
                    stream.Read(bytes, 0, 8);
                    return BitConverter.ToDouble(bytes, 0);
                case VT_NULL:
                    return null;
                case VT_STREAMED_OBJECT:
                    stream = message.BodyStream;
                    ComStreamFromDataStream comStream = new ComStreamFromDataStream(stream);
                    return NativeMethods.OleLoadFromStream(comStream, ref NativeMethods.IID_IUnknown);
                case VT_STORED_OBJECT:
                    throw new NotSupportedException(Res.GetString(Res.StoredObjectsNotSupported));
                default:
                    throw new InvalidOperationException(Res.GetString(Res.InvalidTypeDeserialization));
            }
        }
        public object Read(Message message)
        {
            Stream bodyStream;

            byte[] buffer;
            byte[] buffer2;
            int    num;

            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            switch (message.BodyType)
            {
            case 1:
                return(null);

            case 2:
                bodyStream = message.BodyStream;
                buffer     = new byte[2];
                bodyStream.Read(buffer, 0, 2);
                return(BitConverter.ToInt16(buffer, 0));

            case 3:
                bodyStream = message.BodyStream;
                buffer     = new byte[4];
                bodyStream.Read(buffer, 0, 4);
                return(BitConverter.ToInt32(buffer, 0));

            case 4:
                bodyStream = message.BodyStream;
                buffer     = new byte[4];
                bodyStream.Read(buffer, 0, 4);
                return(BitConverter.ToSingle(buffer, 0));

            case 5:
                bodyStream = message.BodyStream;
                buffer     = new byte[8];
                bodyStream.Read(buffer, 0, 8);
                return(BitConverter.ToDouble(buffer, 0));

            case 6:
                buffer  = message.properties.GetUI1Vector(9);
                buffer2 = new byte[8];
                Array.Copy(buffer, buffer2, 8);
                return(decimal.FromOACurrency(BitConverter.ToInt64(buffer2, 0)));

            case 7:
                buffer  = message.properties.GetUI1Vector(9);
                buffer2 = new byte[8];
                Array.Copy(buffer, buffer2, 8);
                return(new DateTime(BitConverter.ToInt64(buffer2, 0)));

            case 8:
            case 0x1f:
                buffer = message.properties.GetUI1Vector(9);
                num    = message.properties.GetUI4(10) / 2;
                if ((this.internalCharBuffer == null) || (this.internalCharBuffer.Length < num))
                {
                    this.internalCharBuffer = new char[num];
                }
                if (this.unicodeEncoding == null)
                {
                    this.unicodeEncoding = new UnicodeEncoding();
                }
                this.unicodeEncoding.GetChars(buffer, 0, num * 2, this.internalCharBuffer, 0);
                return(new string(this.internalCharBuffer, 0, num));

            case 11:
                buffer  = message.properties.GetUI1Vector(9);
                buffer2 = new byte[1];
                Array.Copy(buffer, buffer2, 1);
                if (buffer[0] == 0)
                {
                    return(false);
                }
                return(true);

            case 0x10:
            case 0x11:
                bodyStream = message.BodyStream;
                buffer     = new byte[1];
                bodyStream.Read(buffer, 0, 1);
                return(buffer[0]);

            case 0x12:
                bodyStream = message.BodyStream;
                buffer     = new byte[2];
                bodyStream.Read(buffer, 0, 2);
                return(BitConverter.ToUInt16(buffer, 0));

            case 0x13:
                bodyStream = message.BodyStream;
                buffer     = new byte[4];
                bodyStream.Read(buffer, 0, 4);
                return(BitConverter.ToUInt32(buffer, 0));

            case 20:
                bodyStream = message.BodyStream;
                buffer     = new byte[8];
                bodyStream.Read(buffer, 0, 8);
                return(BitConverter.ToInt64(buffer, 0));

            case 0x15:
                bodyStream = message.BodyStream;
                buffer     = new byte[8];
                bodyStream.Read(buffer, 0, 8);
                return(BitConverter.ToUInt64(buffer, 0));

            case 30:
                buffer = message.properties.GetUI1Vector(9);
                num    = message.properties.GetUI4(10);
                if ((this.internalCharBuffer == null) || (this.internalCharBuffer.Length < num))
                {
                    this.internalCharBuffer = new char[num];
                }
                if (this.asciiEncoding == null)
                {
                    this.asciiEncoding = new ASCIIEncoding();
                }
                this.asciiEncoding.GetChars(buffer, 0, num, this.internalCharBuffer, 0);
                return(new string(this.internalCharBuffer, 0, num));

            case 0x44:
            {
                ComStreamFromDataStream stream = new ComStreamFromDataStream(message.BodyStream);
                return(System.Messaging.Interop.NativeMethods.OleLoadFromStream(stream, ref System.Messaging.Interop.NativeMethods.IID_IUnknown));
            }

            case 0x45:
                throw new NotSupportedException(Res.GetString("StoredObjectsNotSupported"));

            case 0x48:
                buffer  = message.properties.GetUI1Vector(9);
                buffer2 = new byte[0x10];
                Array.Copy(buffer, buffer2, 0x10);
                return(new Guid(buffer2));

            case 0x1011:
                buffer  = message.properties.GetUI1Vector(9);
                num     = message.properties.GetUI4(10);
                buffer2 = new byte[num];
                Array.Copy(buffer, buffer2, num);
                return(buffer2);
            }
            throw new InvalidOperationException(Res.GetString("InvalidTypeDeserialization"));
        }
 public void Write(Message message, object obj)
 {
     if (message == null)
     {
         throw new ArgumentNullException("message");
     }
     if (obj is string)
     {
         int size = ((string)obj).Length * 2;
         if ((this.internalBuffer == null) || (this.internalBuffer.Length < size))
         {
             this.internalBuffer = new byte[size];
         }
         if (this.unicodeEncoding == null)
         {
             this.unicodeEncoding = new UnicodeEncoding();
         }
         this.unicodeEncoding.GetBytes(((string)obj).ToCharArray(), 0, size / 2, this.internalBuffer, 0);
         message.properties.SetUI1Vector(9, this.internalBuffer);
         message.properties.AdjustSize(9, size);
         message.properties.SetUI4(10, size);
         message.properties.SetUI4(0x2a, 0x1f);
     }
     else if (obj is byte[])
     {
         byte[] sourceArray = (byte[])obj;
         if ((this.internalBuffer == null) || (this.internalBuffer.Length < sourceArray.Length))
         {
             this.internalBuffer = new byte[sourceArray.Length];
         }
         Array.Copy(sourceArray, this.internalBuffer, sourceArray.Length);
         message.properties.SetUI1Vector(9, this.internalBuffer);
         message.properties.AdjustSize(9, sourceArray.Length);
         message.properties.SetUI4(10, sourceArray.Length);
         message.properties.SetUI4(0x2a, 0x1011);
     }
     else if (obj is char[])
     {
         char[] chars = (char[])obj;
         int    num3  = chars.Length * 2;
         if ((this.internalBuffer == null) || (this.internalBuffer.Length < num3))
         {
             this.internalBuffer = new byte[num3];
         }
         if (this.unicodeEncoding == null)
         {
             this.unicodeEncoding = new UnicodeEncoding();
         }
         this.unicodeEncoding.GetBytes(chars, 0, num3 / 2, this.internalBuffer, 0);
         message.properties.SetUI1Vector(9, this.internalBuffer);
         message.properties.SetUI4(10, num3);
         message.properties.SetUI4(0x2a, 0x1f);
     }
     else
     {
         Stream dataStream;
         int    num;
         if (obj is byte)
         {
             dataStream = new MemoryStream(1);
             dataStream.Write(new byte[] { (byte)obj }, 0, 1);
             num = 0x11;
         }
         else if (obj is bool)
         {
             dataStream = new MemoryStream(1);
             if ((bool)obj)
             {
                 dataStream.Write(new byte[] { 0xff }, 0, 1);
             }
             else
             {
                 byte[] buffer = new byte[1];
                 dataStream.Write(buffer, 0, 1);
             }
             num = 11;
         }
         else if (obj is char)
         {
             dataStream = new MemoryStream(2);
             byte[] bytes = BitConverter.GetBytes((char)obj);
             dataStream.Write(bytes, 0, 2);
             num = 0x12;
         }
         else if (obj is decimal)
         {
             dataStream = new MemoryStream(8);
             byte[] buffer3 = BitConverter.GetBytes(decimal.ToOACurrency((decimal)obj));
             dataStream.Write(buffer3, 0, 8);
             num = 6;
         }
         else if (obj is DateTime)
         {
             dataStream = new MemoryStream(8);
             DateTime time    = (DateTime)obj;
             byte[]   buffer4 = BitConverter.GetBytes(time.Ticks);
             dataStream.Write(buffer4, 0, 8);
             num = 7;
         }
         else if (obj is double)
         {
             dataStream = new MemoryStream(8);
             byte[] buffer5 = BitConverter.GetBytes((double)obj);
             dataStream.Write(buffer5, 0, 8);
             num = 5;
         }
         else if (obj is short)
         {
             dataStream = new MemoryStream(2);
             byte[] buffer6 = BitConverter.GetBytes((short)obj);
             dataStream.Write(buffer6, 0, 2);
             num = 2;
         }
         else if (obj is ushort)
         {
             dataStream = new MemoryStream(2);
             byte[] buffer7 = BitConverter.GetBytes((ushort)obj);
             dataStream.Write(buffer7, 0, 2);
             num = 0x12;
         }
         else if (obj is int)
         {
             dataStream = new MemoryStream(4);
             byte[] buffer8 = BitConverter.GetBytes((int)obj);
             dataStream.Write(buffer8, 0, 4);
             num = 3;
         }
         else if (obj is uint)
         {
             dataStream = new MemoryStream(4);
             byte[] buffer9 = BitConverter.GetBytes((uint)obj);
             dataStream.Write(buffer9, 0, 4);
             num = 0x13;
         }
         else if (obj is long)
         {
             dataStream = new MemoryStream(8);
             byte[] buffer10 = BitConverter.GetBytes((long)obj);
             dataStream.Write(buffer10, 0, 8);
             num = 20;
         }
         else if (obj is ulong)
         {
             dataStream = new MemoryStream(8);
             byte[] buffer11 = BitConverter.GetBytes((ulong)obj);
             dataStream.Write(buffer11, 0, 8);
             num = 0x15;
         }
         else if (obj is float)
         {
             dataStream = new MemoryStream(4);
             byte[] buffer12 = BitConverter.GetBytes((float)obj);
             dataStream.Write(buffer12, 0, 4);
             num = 4;
         }
         else if (obj is IPersistStream)
         {
             IPersistStream          persistStream = (IPersistStream)obj;
             ComStreamFromDataStream stream3       = new ComStreamFromDataStream(new MemoryStream());
             System.Messaging.Interop.NativeMethods.OleSaveToStream(persistStream, stream3);
             dataStream = stream3.GetDataStream();
             num        = 0x44;
         }
         else
         {
             if (obj != null)
             {
                 throw new InvalidOperationException(Res.GetString("InvalidTypeSerialization"));
             }
             dataStream = new MemoryStream();
             num        = 1;
         }
         message.BodyStream = dataStream;
         message.BodyType   = num;
     }
 }
        public object Read(Message message)
        {
            Stream bodyStream;
            byte[] buffer;
            byte[] buffer2;
            int num;
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            switch (message.BodyType)
            {
                case 1:
                    return null;

                case 2:
                    bodyStream = message.BodyStream;
                    buffer = new byte[2];
                    bodyStream.Read(buffer, 0, 2);
                    return BitConverter.ToInt16(buffer, 0);

                case 3:
                    bodyStream = message.BodyStream;
                    buffer = new byte[4];
                    bodyStream.Read(buffer, 0, 4);
                    return BitConverter.ToInt32(buffer, 0);

                case 4:
                    bodyStream = message.BodyStream;
                    buffer = new byte[4];
                    bodyStream.Read(buffer, 0, 4);
                    return BitConverter.ToSingle(buffer, 0);

                case 5:
                    bodyStream = message.BodyStream;
                    buffer = new byte[8];
                    bodyStream.Read(buffer, 0, 8);
                    return BitConverter.ToDouble(buffer, 0);

                case 6:
                    buffer = message.properties.GetUI1Vector(9);
                    buffer2 = new byte[8];
                    Array.Copy(buffer, buffer2, 8);
                    return decimal.FromOACurrency(BitConverter.ToInt64(buffer2, 0));

                case 7:
                    buffer = message.properties.GetUI1Vector(9);
                    buffer2 = new byte[8];
                    Array.Copy(buffer, buffer2, 8);
                    return new DateTime(BitConverter.ToInt64(buffer2, 0));

                case 8:
                case 0x1f:
                    buffer = message.properties.GetUI1Vector(9);
                    num = message.properties.GetUI4(10) / 2;
                    if ((this.internalCharBuffer == null) || (this.internalCharBuffer.Length < num))
                    {
                        this.internalCharBuffer = new char[num];
                    }
                    if (this.unicodeEncoding == null)
                    {
                        this.unicodeEncoding = new UnicodeEncoding();
                    }
                    this.unicodeEncoding.GetChars(buffer, 0, num * 2, this.internalCharBuffer, 0);
                    return new string(this.internalCharBuffer, 0, num);

                case 11:
                    buffer = message.properties.GetUI1Vector(9);
                    buffer2 = new byte[1];
                    Array.Copy(buffer, buffer2, 1);
                    if (buffer[0] == 0)
                    {
                        return false;
                    }
                    return true;

                case 0x10:
                case 0x11:
                    bodyStream = message.BodyStream;
                    buffer = new byte[1];
                    bodyStream.Read(buffer, 0, 1);
                    return buffer[0];

                case 0x12:
                    bodyStream = message.BodyStream;
                    buffer = new byte[2];
                    bodyStream.Read(buffer, 0, 2);
                    return BitConverter.ToUInt16(buffer, 0);

                case 0x13:
                    bodyStream = message.BodyStream;
                    buffer = new byte[4];
                    bodyStream.Read(buffer, 0, 4);
                    return BitConverter.ToUInt32(buffer, 0);

                case 20:
                    bodyStream = message.BodyStream;
                    buffer = new byte[8];
                    bodyStream.Read(buffer, 0, 8);
                    return BitConverter.ToInt64(buffer, 0);

                case 0x15:
                    bodyStream = message.BodyStream;
                    buffer = new byte[8];
                    bodyStream.Read(buffer, 0, 8);
                    return BitConverter.ToUInt64(buffer, 0);

                case 30:
                    buffer = message.properties.GetUI1Vector(9);
                    num = message.properties.GetUI4(10);
                    if ((this.internalCharBuffer == null) || (this.internalCharBuffer.Length < num))
                    {
                        this.internalCharBuffer = new char[num];
                    }
                    if (this.asciiEncoding == null)
                    {
                        this.asciiEncoding = new ASCIIEncoding();
                    }
                    this.asciiEncoding.GetChars(buffer, 0, num, this.internalCharBuffer, 0);
                    return new string(this.internalCharBuffer, 0, num);

                case 0x44:
                {
                    ComStreamFromDataStream stream = new ComStreamFromDataStream(message.BodyStream);
                    return System.Messaging.Interop.NativeMethods.OleLoadFromStream(stream, ref System.Messaging.Interop.NativeMethods.IID_IUnknown);
                }
                case 0x45:
                    throw new NotSupportedException(Res.GetString("StoredObjectsNotSupported"));

                case 0x48:
                    buffer = message.properties.GetUI1Vector(9);
                    buffer2 = new byte[0x10];
                    Array.Copy(buffer, buffer2, 0x10);
                    return new Guid(buffer2);

                case 0x1011:
                    buffer = message.properties.GetUI1Vector(9);
                    num = message.properties.GetUI4(10);
                    buffer2 = new byte[num];
                    Array.Copy(buffer, buffer2, num);
                    return buffer2;
            }
            throw new InvalidOperationException(Res.GetString("InvalidTypeDeserialization"));
        }
 public void Write(Message message, object obj)
 {
     if (message == null)
     {
         throw new ArgumentNullException("message");
     }
     if (obj is string)
     {
         int size = ((string) obj).Length * 2;
         if ((this.internalBuffer == null) || (this.internalBuffer.Length < size))
         {
             this.internalBuffer = new byte[size];
         }
         if (this.unicodeEncoding == null)
         {
             this.unicodeEncoding = new UnicodeEncoding();
         }
         this.unicodeEncoding.GetBytes(((string) obj).ToCharArray(), 0, size / 2, this.internalBuffer, 0);
         message.properties.SetUI1Vector(9, this.internalBuffer);
         message.properties.AdjustSize(9, size);
         message.properties.SetUI4(10, size);
         message.properties.SetUI4(0x2a, 0x1f);
     }
     else if (obj is byte[])
     {
         byte[] sourceArray = (byte[]) obj;
         if ((this.internalBuffer == null) || (this.internalBuffer.Length < sourceArray.Length))
         {
             this.internalBuffer = new byte[sourceArray.Length];
         }
         Array.Copy(sourceArray, this.internalBuffer, sourceArray.Length);
         message.properties.SetUI1Vector(9, this.internalBuffer);
         message.properties.AdjustSize(9, sourceArray.Length);
         message.properties.SetUI4(10, sourceArray.Length);
         message.properties.SetUI4(0x2a, 0x1011);
     }
     else if (obj is char[])
     {
         char[] chars = (char[]) obj;
         int num3 = chars.Length * 2;
         if ((this.internalBuffer == null) || (this.internalBuffer.Length < num3))
         {
             this.internalBuffer = new byte[num3];
         }
         if (this.unicodeEncoding == null)
         {
             this.unicodeEncoding = new UnicodeEncoding();
         }
         this.unicodeEncoding.GetBytes(chars, 0, num3 / 2, this.internalBuffer, 0);
         message.properties.SetUI1Vector(9, this.internalBuffer);
         message.properties.SetUI4(10, num3);
         message.properties.SetUI4(0x2a, 0x1f);
     }
     else
     {
         Stream dataStream;
         int num;
         if (obj is byte)
         {
             dataStream = new MemoryStream(1);
             dataStream.Write(new byte[] { (byte) obj }, 0, 1);
             num = 0x11;
         }
         else if (obj is bool)
         {
             dataStream = new MemoryStream(1);
             if ((bool) obj)
             {
                 dataStream.Write(new byte[] { 0xff }, 0, 1);
             }
             else
             {
                 byte[] buffer = new byte[1];
                 dataStream.Write(buffer, 0, 1);
             }
             num = 11;
         }
         else if (obj is char)
         {
             dataStream = new MemoryStream(2);
             byte[] bytes = BitConverter.GetBytes((char) obj);
             dataStream.Write(bytes, 0, 2);
             num = 0x12;
         }
         else if (obj is decimal)
         {
             dataStream = new MemoryStream(8);
             byte[] buffer3 = BitConverter.GetBytes(decimal.ToOACurrency((decimal) obj));
             dataStream.Write(buffer3, 0, 8);
             num = 6;
         }
         else if (obj is DateTime)
         {
             dataStream = new MemoryStream(8);
             DateTime time = (DateTime) obj;
             byte[] buffer4 = BitConverter.GetBytes(time.Ticks);
             dataStream.Write(buffer4, 0, 8);
             num = 7;
         }
         else if (obj is double)
         {
             dataStream = new MemoryStream(8);
             byte[] buffer5 = BitConverter.GetBytes((double) obj);
             dataStream.Write(buffer5, 0, 8);
             num = 5;
         }
         else if (obj is short)
         {
             dataStream = new MemoryStream(2);
             byte[] buffer6 = BitConverter.GetBytes((short) obj);
             dataStream.Write(buffer6, 0, 2);
             num = 2;
         }
         else if (obj is ushort)
         {
             dataStream = new MemoryStream(2);
             byte[] buffer7 = BitConverter.GetBytes((ushort) obj);
             dataStream.Write(buffer7, 0, 2);
             num = 0x12;
         }
         else if (obj is int)
         {
             dataStream = new MemoryStream(4);
             byte[] buffer8 = BitConverter.GetBytes((int) obj);
             dataStream.Write(buffer8, 0, 4);
             num = 3;
         }
         else if (obj is uint)
         {
             dataStream = new MemoryStream(4);
             byte[] buffer9 = BitConverter.GetBytes((uint) obj);
             dataStream.Write(buffer9, 0, 4);
             num = 0x13;
         }
         else if (obj is long)
         {
             dataStream = new MemoryStream(8);
             byte[] buffer10 = BitConverter.GetBytes((long) obj);
             dataStream.Write(buffer10, 0, 8);
             num = 20;
         }
         else if (obj is ulong)
         {
             dataStream = new MemoryStream(8);
             byte[] buffer11 = BitConverter.GetBytes((ulong) obj);
             dataStream.Write(buffer11, 0, 8);
             num = 0x15;
         }
         else if (obj is float)
         {
             dataStream = new MemoryStream(4);
             byte[] buffer12 = BitConverter.GetBytes((float) obj);
             dataStream.Write(buffer12, 0, 4);
             num = 4;
         }
         else if (obj is IPersistStream)
         {
             IPersistStream persistStream = (IPersistStream) obj;
             ComStreamFromDataStream stream3 = new ComStreamFromDataStream(new MemoryStream());
             System.Messaging.Interop.NativeMethods.OleSaveToStream(persistStream, stream3);
             dataStream = stream3.GetDataStream();
             num = 0x44;
         }
         else
         {
             if (obj != null)
             {
                 throw new InvalidOperationException(Res.GetString("InvalidTypeSerialization"));
             }
             dataStream = new MemoryStream();
             num = 1;
         }
         message.BodyStream = dataStream;
         message.BodyType = num;
     }
 }