Exemple #1
0
        private int WriteField(Context context, Integer field, object fieldValue)
        {
            byte[] buffer = context.Buffer;
            int    length = field.LengthSpecified ? field.Length : 4;
            bool   signed = field.Signed;

            if (field.GetType() == typeof(Int8))
            {
                length = 1;
                signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int16))
            {
                length = 2;
                signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int32))
            {
                length = 4;
                signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int64))
            {
                length = 8;
                signed = true;
            }
            else if (field.GetType() == typeof(UInt8))
            {
                length = 1;
                signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt16))
            {
                length = 2;
                signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt32))
            {
                length = 4;
                signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt64))
            {
                length = 8;
                signed = false;
            }
            if (buffer == null)
            {
                return(length);
            }
            if ((buffer.Length - context.Index) < length)
            {
                throw new InvalidDataToWriteException("Unexpected end of buffer.");
            }
            byte[] bytes = null;
            if (!signed)
            {
                switch (length)
                {
                case 1:
                    bytes = new byte[] { Convert.ToByte(fieldValue) };
                    goto Label_0205;

                case 2:
                    bytes = BitConverter.GetBytes(Convert.ToUInt16(fieldValue));
                    goto Label_0205;

                case 4:
                    bytes = BitConverter.GetBytes(Convert.ToUInt32(fieldValue));
                    goto Label_0205;

                case 8:
                    bytes = BitConverter.GetBytes(Convert.ToUInt64(fieldValue));
                    goto Label_0205;
                }
            }
            else
            {
                switch (length)
                {
                case 1:
                {
                    bytes = new byte[1];
                    sbyte num2 = Convert.ToSByte(fieldValue);
                    if (num2 >= 0)
                    {
                        bytes[0] = (byte)num2;
                    }
                    else
                    {
                        bytes[0] = (byte)((0xff + num2) + 1);
                    }
                    goto Label_0205;
                }

                case 2:
                    bytes = BitConverter.GetBytes(Convert.ToInt16(fieldValue));
                    goto Label_0205;

                case 4:
                    bytes = BitConverter.GetBytes(Convert.ToInt32(fieldValue));
                    goto Label_0205;

                case 8:
                    bytes = BitConverter.GetBytes(Convert.ToInt64(fieldValue));
                    goto Label_0205;
                }
                bytes = (byte[])fieldValue;
                goto Label_0205;
            }
            bytes = (byte[])fieldValue;
Label_0205:
            if (context.BigEndian)
            {
                base.SwapBytes(bytes, 0, length);
            }
            for (int i = 0; i < bytes.Length; i++)
            {
                buffer[context.Index + i] = bytes[i];
            }
            return(length);
        }