示例#1
0
        public override object ConvertToObject(byte[] bytes, object parentObj)
        {
            var collectionSizePropertyInfo = parentObj.GetType().GetProperty(CollectionSizePropertyName);
            var collectionPropertyInfo     = parentObj.GetType().GetProperty(CollectionPropertyName);

            var collectionSizeString    = collectionSizePropertyInfo.GetValue(parentObj, null).ToString();
            var collectionPropertyValue = collectionPropertyInfo.GetValue(parentObj, null);

            var collectionSize = int.Parse(collectionSizeString);

            if (collectionSize * SingleDataSize != bytes.Length)
            {
                throw new ArgumentNullException();
            }

            for (int i = 0; i < collectionSize; i++)
            {
                var elementBytes = new byte[SingleDataSize];
                for (int j = 0; j < SingleDataSize; j++)
                {
                    elementBytes[i] = bytes[i * SingleDataSize + j];
                }

                var elementInstance = DataPackageConvert.ToObject(elementBytes, CollectionElementType);

                DataFieldType.InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, collectionPropertyValue, new[] { elementInstance });
            }

            return(collectionPropertyValue);
        }
示例#2
0
        public override byte[] ConvertToBytes <T>(T dataFieldValue)
        {
            var itemBytesList = new List <byte[]>();
            var allBytesSize  = 0;

            if (dataFieldValue is ICollection)
            {
                var collection = dataFieldValue as ICollection;
                foreach (var item in collection)
                {
                    var itemBytes = new byte[] { };
                    if (item is string)
                    {
                        itemBytes = Encoding.ASCII.GetBytes(item.ToString());
                    }
                    else
                    {
                        itemBytes = DataPackageConvert.ToDataPackage(item);
                    }
                    allBytesSize += itemBytes.Length;
                    itemBytesList.Add(itemBytes);
                }
            }

            var allBytes   = new byte[allBytesSize];
            var bytesIndex = 0;

            foreach (var itemBytes in itemBytesList)
            {
                Array.Copy(itemBytes, 0, allBytes, bytesIndex, itemBytes.Length);

                bytesIndex += itemBytes.Length;
            }

            return(allBytes);
        }
 public override object ConvertToObject(byte[] bytes)
 {
     return(DataPackageConvert.ToObject(bytes, DataFieldType));
 }
 public override byte[] ConvertToBytes <T>(T dataFieldValue)
 {
     return(DataPackageConvert.ToDataPackage(dataFieldValue));
 }