Ejemplo n.º 1
0
        public override TObject Deserialize(Stream stream)
        {
            var  theObject = Activator.CreateInstance <TObject>();
            Type enumerableType;

            foreach (var property in _properties)
            {
                if (TypesInfo.SystemTypes.Contains(property.PropertyType))
                {
                    ReadObjectProperty(stream, property, theObject);
                }
                else if (TypesInfo.IsNullable(property.PropertyType))
                {
                    ReadObjectNullableProperty(stream, property, theObject);
                }
                else if (TypesInfo.IsSupportedCollectionType(property.PropertyType, out enumerableType))
                {
                    var lengthBytes = new byte[sizeof(int)];
                    stream.Read(lengthBytes, 0, lengthBytes.Length);
                    var arrayLength = BitConverter.ToInt32(lengthBytes, 0);
                    if (arrayLength > NullBytesCout)
                    {
                        var elementType = enumerableType == typeof(Array)
                            ? property.PropertyType.GetElementType()
                            : enumerableType.GetGenericArguments()[0];

                        var array = Array.CreateInstance(elementType, arrayLength);
                        for (var i = 0; i < arrayLength; i++)
                        {
                            var element = ReadValue(stream, elementType);
                            array.SetValue(element, i);
                        }

                        if (enumerableType == typeof(Array))
                        {
                            property.SetValue(theObject, array);
                        }
                        else
                        {
                            var ctor       = property.PropertyType.GetConstructor(new [] { enumerableType });
                            var enumerable = ctor.Invoke(new [] { array });
                            property.SetValue(theObject, enumerable);
                        }
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }

            return(theObject);
        }
Ejemplo n.º 2
0
        public override void Serialize(TObject theObject, Stream stream)
        {
            // properties
            foreach (var property in _properties)
            {
                Type enumerableType;
                if (TypesInfo.SystemTypes.Contains(property.PropertyType))
                {
                    WriteObjectProperty(stream, property, theObject);
                }
                else if (TypesInfo.IsNullable(property.PropertyType))
                {
                    WriteObjectNullableProperty(stream, property, theObject);
                }
                else if (TypesInfo.IsSupportedCollectionType(property.PropertyType, out enumerableType))
                {
                    var elementType = enumerableType == typeof(Array)
                        ? property.PropertyType.GetElementType()
                        : enumerableType.GetGenericArguments()[0];
                    var array = enumerableType == typeof(Array)
                        ? (Array)property.GetValue(theObject)
                        : (Array)EnumerablesInfo
                                .GetToArrayMethod(enumerableType)
                                .Invoke(null, new object[] { property.GetValue(theObject) });
                    if (array != null)
                    {
                        var arrayLengthBytes = BitConverter.GetBytes(array.Length);
                        stream.Write(arrayLengthBytes, 0, arrayLengthBytes.Length);

                        for (var i = 0; i < array.Length; i++)
                        {
                            var arrayValue = array.GetValue(i);
                            WriteValueBytes(stream, arrayValue, elementType);
                        }
                    }
                    else
                    {
                        var arrayLengthBytes = BitConverter.GetBytes(NullBytesCout);
                        stream.Write(arrayLengthBytes, 0, arrayLengthBytes.Length);
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
        }