/// <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;
        }
 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;
     }
 }
        /// <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;
        }
 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;
     }
 }