/// <summary>
        /// Writes a integer value from to the buffer.
        /// </summary>
        private int WriteField(TsCCpxContext context, Integer field, object fieldValue)
        {
            byte[] buffer = context.Buffer;

            // initialize serialization paramters.
            int  length = (field.LengthSpecified) ? (int)field.Length : 4;
            bool signed = field.Signed;

            // apply defaults for built in types.
            if (field.GetType() == typeof(Int8))
            {
                length = 1; signed = true;
            }
            else if (field.GetType() == typeof(Int16))
            {
                length = 2; signed = true;
            }
            else if (field.GetType() == typeof(Int32))
            {
                length = 4; signed = true;
            }
            else if (field.GetType() == typeof(Int64))
            {
                length = 8; signed = true;
            }
            else if (field.GetType() == typeof(UInt8))
            {
                length = 1; signed = false;
            }
            else if (field.GetType() == typeof(UInt16))
            {
                length = 2; signed = false;
            }
            else if (field.GetType() == typeof(UInt32))
            {
                length = 4; signed = false;
            }
            else if (field.GetType() == typeof(UInt64))
            {
                length = 8; signed = false;
            }

            // only write to the buffer if it has been allocated.
            if (buffer != null)
            {
                // check if there is enough data left.
                if (buffer.Length - context.Index < length)
                {
                    throw new TsCCpxInvalidDataToWriteException("Unexpected end of buffer.");
                }

                // copy and swap bytes if required.
                byte[] bytes = null;

                if (signed)
                {
                    switch (length)
                    {
                    case 1:
                    {
                        bytes = new byte[1];

                        sbyte value = Convert.ToSByte(fieldValue);

                        if (value < 0)
                        {
                            bytes[0] = (byte)(Byte.MaxValue + value + 1);
                        }
                        else
                        {
                            bytes[0] = (byte)value;
                        }

                        break;
                    }

                    case 2: { bytes = BitConverter.GetBytes(Convert.ToInt16(fieldValue)); break; }

                    case 4: { bytes = BitConverter.GetBytes(Convert.ToInt32(fieldValue)); break; }

                    case 8: { bytes = BitConverter.GetBytes(Convert.ToInt64(fieldValue)); break; }

                    default: { bytes = (byte[])fieldValue; break; }
                    }
                }
                else
                {
                    switch (length)
                    {
                    case 1: { bytes = new byte[] { Convert.ToByte(fieldValue) }; break; }

                    case 2: { bytes = BitConverter.GetBytes(Convert.ToUInt16(fieldValue)); break; }

                    case 4: { bytes = BitConverter.GetBytes(Convert.ToUInt32(fieldValue)); break; }

                    case 8: { bytes = BitConverter.GetBytes(Convert.ToUInt64(fieldValue)); break; }

                    default: { bytes = (byte[])fieldValue; break; }
                    }
                }

                // copy and swap bytes.
                if (context.BigEndian)
                {
                    SwapBytes(bytes, 0, length);
                }

                // write bytes to buffer.
                for (int ii = 0; ii < bytes.Length; ii++)
                {
                    buffer[context.Index + ii] = bytes[ii];
                }
            }

            return(length);
        }