Cauterize.CauterizeGroupFormatter.Deserialize C# (CSharp) Method

Deserialize() public method

public Deserialize ( Stream serializationStream, Type t ) : object
serializationStream Stream
t System.Type
return object
        public override object Deserialize(Stream serializationStream, Type t)
        {
            var ret = t.GetConstructor(new Type[] {}).Invoke(new object[] {});
            var typeProp = OrderAttribute.GetPropertyByOrder(t, 0);
            var enumFormatter = _typeFormatterFactory.GetFormatter(typeProp.PropertyType);
            var enumValue = enumFormatter.Deserialize(serializationStream, typeProp.PropertyType);
            typeProp.SetValue(ret, enumValue, null);
            var index = ((int) enumValue) + 1;
            var prop = OrderAttribute.GetPropertyByOrder(t, index);
            if (prop != null)
            {
                var subType = prop.PropertyType;
                var subFormatter = _typeFormatterFactory.GetFormatter(subType);
                var subObj = subFormatter.Deserialize(serializationStream, subType);
                prop.SetValue(ret, subObj, null);
            }
            return ret;
        }

Usage Example

 public void TestDeserialize()
 {
     var stream = new MemoryStream();
     var factory = new Mock<CauterizeTypeFormatterFactory>();
     var enumFormatter = new Mock<ICauterizeTypeFormatter>();
     var intFormatter = new Mock<ICauterizeTypeFormatter>();
     factory.Setup(f => f.GetFormatter(It.IsAny<Type>())).Returns((Type t) =>
         {
             if (t == typeof (TestGroupType))
             {
                 return enumFormatter.Object;
             }
             else if (t == typeof (int))
             {
                 return intFormatter.Object;
             }
             else
             {
                 return null;
             }
         });
     enumFormatter.Setup(f => f.Deserialize(stream, typeof (TestGroupType)))
                  .Returns(TestGroupType.TestGroupTypeFoo);
     intFormatter.Setup(f => f.Deserialize(stream, typeof (int)))
                 .Returns(1024);
     var formatter = new CauterizeGroupFormatter(factory.Object);
     var result = (TestGroup) formatter.Deserialize(stream, typeof (TestGroup));
     Assert.AreEqual(TestGroupType.TestGroupTypeFoo, result.Type);
     Assert.AreEqual(1024, result.Foo);
 }