/// <summary> /// Initializes a new instance of the DataElement class. /// </summary> /// <param name="type">data element type</param> /// <param name="data">Specifies the data of the element.</param> public DataElement(DataElementType type, DataElementData data) : base(StreamObjectTypeHeaderStart.DataElement) { if (!dataElementDataTypeMapping.Keys.Contains(type)) { throw new InvalidOperationException("Invalid argument type value" + (int)type); } this.DataElementType = type; this.Data = data; this.DataElementExtendedGUID = new ExGuid(SequenceNumberGenerator.GetCurrentSerialNumber(), Guid.NewGuid()); this.SerialNumber = new SerialNumber(Guid.NewGuid(), SequenceNumberGenerator.GetCurrentSerialNumber()); }
/// <summary> /// Used to de-serialize the element. /// </summary> /// <param name="byteArray">A Byte array</param> /// <param name="currentIndex">Start position</param> /// <param name="lengthOfItems">The length of the items</param> protected override void DeserializeItemsFromByteArray(byte[] byteArray, ref int currentIndex, int lengthOfItems) { int index = currentIndex; try { this.DataElementExtendedGUID = BasicObject.Parse <ExGuid>(byteArray, ref index); this.SerialNumber = BasicObject.Parse <SerialNumber>(byteArray, ref index); this.DataElementType = (DataElementType)BasicObject.Parse <Compact64bitInt>(byteArray, ref index).DecodedValue; } catch (BasicObjectParseErrorException e) { throw new DataElementParseErrorException(index, e); } if (index - currentIndex != lengthOfItems) { throw new DataElementParseErrorException(currentIndex, "Failed to check the data element header length, whose value does not cover the DataElementExtendedGUID, SerialNumber and DataElementType", null); } if (dataElementDataTypeMapping.ContainsKey(this.DataElementType)) { this.Data = Activator.CreateInstance(dataElementDataTypeMapping[this.DataElementType]) as DataElementData; try { index += this.Data.DeserializeDataElementDataFromByteArray(byteArray, index); } catch (BasicObjectParseErrorException e1) { throw new DataElementParseErrorException(index, e1); } catch (StreamObjectParseErrorException e2) { throw new DataElementParseErrorException(index, e2); } } else { throw new DataElementParseErrorException(index, "Failed to create specific data element instance with the type " + this.DataElementType, null); } currentIndex = index; }