public void EncodeArray(IList <int> value, int expectedLength, byte[] expectedValue)
 {
     using (var stream = new MemoryStream())
         using (var encoder = new BinaryEncoder(stream))
         {
             encoder.WriteArray(value, (s, e) => s.WriteInt(e));
             Assert.AreEqual(expectedLength, stream.Position, "Encode offset error");
             Assert.AreEqual(expectedValue, stream.GetBuffer().AsSpan(0, expectedLength).ToArray());
         }
 }
示例#2
0
 public void TestArray(IList <string> expected)
 {
     using (var stream = new MemoryStream())
         using (var encoder = new BinaryEncoder(stream))
             using (var decoder = new BinaryDecoder(stream))
             {
                 encoder.WriteArray(expected, (s, e) => s.WriteString(e));
                 stream.Seek(0, SeekOrigin.Begin);
                 var actual = decoder.ReadArray(s => s.ReadString());
                 Assert.AreEqual(expected, actual);
             }
 }
示例#3
0
        /// <summary>
        /// Encodes field value as RawData
        /// </summary>
        /// <param name="binaryEncoder"></param>
        /// <param name="field"></param>
        private void EncodeFieldAsRawData(BinaryEncoder binaryEncoder, Field field)
        {
            try
            {
                // 01 RawData Field Encoding
                var variant = field.Value.WrappedValue;

                if (variant.TypeInfo == null || variant.TypeInfo.BuiltInType == BuiltInType.Null)
                {
                    return;
                }
                object valueToEncode = variant.Value;

                if (field.FieldMetaData.ValueRank == ValueRanks.Scalar)
                {
                    switch ((BuiltInType)field.FieldMetaData.BuiltInType)
                    {
                    case BuiltInType.Boolean:
                        binaryEncoder.WriteBoolean("Bool", Convert.ToBoolean(valueToEncode));
                        break;

                    case BuiltInType.SByte:
                        binaryEncoder.WriteSByte("SByte", Convert.ToSByte(valueToEncode));
                        break;

                    case BuiltInType.Byte:
                        binaryEncoder.WriteByte("Byte", Convert.ToByte(valueToEncode));
                        break;

                    case BuiltInType.Int16:
                        binaryEncoder.WriteInt16("Int16", Convert.ToInt16(valueToEncode));
                        break;

                    case BuiltInType.UInt16:
                        binaryEncoder.WriteUInt16("UInt16", Convert.ToUInt16(valueToEncode));
                        break;

                    case BuiltInType.Int32:
                        binaryEncoder.WriteInt32("Int32", Convert.ToInt32(valueToEncode));
                        break;

                    case BuiltInType.UInt32:
                        binaryEncoder.WriteUInt32("UInt32", Convert.ToUInt32(valueToEncode));
                        break;

                    case BuiltInType.Int64:
                        binaryEncoder.WriteInt64("Int64", Convert.ToInt64(valueToEncode));
                        break;

                    case BuiltInType.UInt64:
                        binaryEncoder.WriteUInt64("UInt64", Convert.ToUInt64(valueToEncode));
                        break;

                    case BuiltInType.Float:
                        binaryEncoder.WriteFloat("Float", Convert.ToSingle(valueToEncode));
                        break;

                    case BuiltInType.Double:
                        binaryEncoder.WriteDouble("Double", Convert.ToDouble(valueToEncode));
                        break;

                    case BuiltInType.DateTime:
                        binaryEncoder.WriteDateTime("DateTime", Convert.ToDateTime(valueToEncode));
                        break;

                    case BuiltInType.Guid:
                        binaryEncoder.WriteGuid("GUID", (Uuid)valueToEncode);
                        break;

                    case BuiltInType.String:
                        binaryEncoder.WriteString("String", valueToEncode as string);
                        break;

                    case BuiltInType.ByteString:
                        binaryEncoder.WriteByteString("ByteString", (byte[])valueToEncode);
                        break;

                    case BuiltInType.QualifiedName:
                        binaryEncoder.WriteQualifiedName("QualifiedName", valueToEncode as QualifiedName);
                        break;

                    case BuiltInType.LocalizedText:
                        binaryEncoder.WriteLocalizedText("LocalizedText", valueToEncode as LocalizedText);
                        break;

                    case BuiltInType.NodeId:
                        binaryEncoder.WriteNodeId("NodeId", valueToEncode as NodeId);
                        break;

                    case BuiltInType.ExpandedNodeId:
                        binaryEncoder.WriteExpandedNodeId("ExpandedNodeId", valueToEncode as ExpandedNodeId);
                        break;

                    case BuiltInType.StatusCode:
                        binaryEncoder.WriteStatusCode("StatusCode", (StatusCode)valueToEncode);
                        break;

                    case BuiltInType.XmlElement:
                        binaryEncoder.WriteXmlElement("XmlElement", valueToEncode as XmlElement);
                        break;

                    case BuiltInType.Enumeration:
                        binaryEncoder.WriteInt32("Enumeration", Convert.ToInt32(valueToEncode));
                        break;

                    case BuiltInType.ExtensionObject:
                        binaryEncoder.WriteExtensionObject("ExtensionObject", valueToEncode as ExtensionObject);
                        break;
                    }
                }
                else if (field.FieldMetaData.ValueRank >= ValueRanks.OneDimension)
                {
                    binaryEncoder.WriteArray(null, valueToEncode, field.FieldMetaData.ValueRank, (BuiltInType)field.FieldMetaData.BuiltInType);
                }
            }
            catch (Exception ex)
            {
                Utils.Trace(ex, "Error encoding field {0}.", field.FieldMetaData.Name);
            }
        }