Exemplo n.º 1
0
        public void ShouldSerializeTypesList()
        {
            _model.Add(Assembly.GetExecutingAssembly(), true, true, true);
            Type[] types  = _model.Types;
            Type[] cloned = (Type[])_model.DeepClone(types);

            Assert.IsTrue(cloned.SequenceEqual(types));
        }
        public T DeepClone <T>(object objectToClone)
        {
            if (objectToClone == null)
            {
                return(default(T));
            }

            EnsureRuntimeTypeModelCreated(objectToClone.GetType());
            return((T)_runtimeTypeModel.DeepClone(objectToClone));
        }
Exemplo n.º 3
0
        public void TestNullArrays([Values(false, true)] bool compiled)
        {
            _model             = TypeModel.Create();
            _model.AutoCompile = compiled;
            var obj   = new ArrayContainerClass();
            var clone = _model.DeepClone(obj);

            Assert.IsNull(clone.Data);
            Assert.IsNull(clone.Data2);
        }
Exemplo n.º 4
0
        public void Execute()
        {
            RuntimeTypeModel model = RuntimeTypeModel.Create();

            var original = new Custom <string> {
                "C#"
            };
            var clone = (Custom <string>)model.DeepClone(original);

            Assert.Single(clone);
            Assert.Equal("C#", clone.Single());
        }
Exemplo n.º 5
0
        private void CheckInherited()
        {
            var obj = new TestInherited()
            {
                PublicProperty = 1, PublicProperty2 = 2, PublicProperty3 = 3
            };
            var clone = (TestInherited)_model.DeepClone(obj);

            Assert.AreEqual(clone.PublicProperty, obj.PublicProperty);
            Assert.AreEqual(clone.PublicProperty2, obj.PublicProperty2);
            Assert.AreEqual(clone.PublicProperty3, obj.PublicProperty3);
            clone.CheckPrivateValues(0, 0);
        }
Exemplo n.º 6
0
        public void TestEmptyLists([Values(false, true)] bool compiled)
        {
            _model             = TypeModel.Create();
            _model.AutoCompile = compiled;
            var obj = new ListContainerClass()
            {
                Data = new List <Data>(), Data2 = new List <int>()
            };
            var clone = _model.DeepClone(obj);

            Assert.IsNotNull(clone.Data);
            Assert.AreEqual(0, clone.Data.Count);
            Assert.AreEqual(0, clone.Data2.Count);
        }
        public void Execute()
        {
            var obj = new Inherited()
            {
                PublicProperty = 1, PublicProperty2 = 2, PublicProperty3 = 3, PublicProperty4 = 4, PublicProperty5 = 5
            };
            var clone = (Inherited)_model.DeepClone(obj);

            Assert.AreEqual(obj.PublicProperty, clone.PublicProperty);
            Assert.AreEqual(obj.PublicProperty2, clone.PublicProperty2);
            Assert.AreEqual(obj.PublicProperty3, clone.PublicProperty3);
            Assert.AreEqual(obj.PublicProperty5, clone.PublicProperty5);
            Assert.AreEqual(0, clone.PublicProperty4);
        }
Exemplo n.º 8
0
        public void TestEmptyArrays([Values(false, true)] bool compiled)
        {
            _model             = TypeModel.Create();
            _model.AutoCompile = compiled;
            var obj = new ArrayContainerClass()
            {
                Data = new Data[0], Data2 = new int[0]
            };
            var clone = _model.DeepClone(obj);

            Assert.IsNotNull(clone.Data);
            Assert.AreEqual(0, clone.Data.Length);
            Assert.AreEqual(0, clone.Data2.Length);
        }
Exemplo n.º 9
0
        public static void Main(string[] args)
        {
            RuntimeTypeModel model = TypeModel.Create();

            // There seems to be a bug in protobuf-net,
            // if I don't do those deepclones they doesn't get registered...
            if (model.CanSerialize(typeof(Character)))
            {
                Character state = new Character();
                model.DeepClone(state);
                Console.WriteLine("deep cloned Character");
            }
            // There seems to be a bug in protobuf-net,
            // if I don't do those deepclones they doesn't get registered...
            if (model.CanSerialize(typeof(HUD)))
            {
                HUD state = new HUD();
                model.DeepClone(state);
                Console.WriteLine("deep cloned HUD");
            }

            Console.WriteLine("compiling..");
            model.Compile(SerializationConstants.StorageSerializerName, SerializationConstants.StorageDllName);
        }
Exemplo n.º 10
0
        public void ShouldAddMissingAsImplicitProperties()
        {
            var obj = new TestClass()
            {
                PublicProperty = 1, PublicProperty2 = 2, PublicField = 3
            };

            obj.SetPrivateValues(4, 5);
            var clone = (TestClass)_model.DeepClone(obj);

            Assert.AreEqual(clone.PublicField, 0);
            Assert.AreEqual(clone.PublicProperty, obj.PublicProperty);
            Assert.AreEqual(clone.PublicProperty2, obj.PublicProperty2);
            clone.CheckPrivateValues(0, 0);
        }
Exemplo n.º 11
0
        private void RoundtripTypeWithColor(RuntimeTypeModel model)
        {
            var orig = new TypeWithColor
            {
                Color = new Color {
                    A = 1, R = 2, G = 3, B = 4
                }
            };
            var clone = (TypeWithColor)model.DeepClone(orig);

            Assert.Equal(1, clone.Color.A);
            Assert.Equal(2, clone.Color.R);
            Assert.Equal(3, clone.Color.G);
            Assert.Equal(4, clone.Color.B);
        }
Exemplo n.º 12
0
        public void TestNonEmptyArrays([Values(false, true)] bool compiled)
        {
            _model             = TypeModel.Create();
            _model.AutoCompile = compiled;
            var obj = new ArrayContainerClass()
            {
                Data = new[] { new Data()
                               {
                                   PublicProperty = 234, PublicProperty2 = 567
                               } }, Data2 = new[] { 1 }
            };
            var clone = _model.DeepClone(obj);

            Assert.IsNotNull(clone.Data);
            Assert.AreEqual(1, clone.Data.Length);
            Assert.AreEqual(1, clone.Data2.Length);
        }
Exemplo n.º 13
0
        public void TestOrderLineGetDeserializedAndAttachedToOrder()
        {
            byte[] fileBytes = File.ReadAllBytes(@"NWind\nwind.proto.bin");

            RuntimeTypeModel ordersModel = TypeModel.Create();

            ordersModel.AutoCompile = false;

            Database     database = (Database)ordersModel.Deserialize(new MemoryStream(fileBytes), null, typeof(Database));
            List <Order> orders   = database.Orders;

            DbMetrics("From File", orders);

            var roundTrippedOrders = (List <Order>)ordersModel.DeepClone(orders);

            Assert.AreNotSame(orders, roundTrippedOrders);
            DbMetrics("Round trip", roundTrippedOrders);
            Assert.AreEqual(orders.SelectMany(o => o.Lines).Count(),
                            roundTrippedOrders.SelectMany(o => o.Lines).Count(), "total count");
        }
Exemplo n.º 14
0
        public static void Main(string[] args)
        {
            RuntimeTypeModel model = TypeModel.Create();     //RuntimeTypeModel.Default;

            // Add new derived models
//			Console.WriteLine("1");
//			model[typeof(Model)].AddSubType(100, typeof(WorldModel));
            Console.WriteLine("1");
            model[typeof(NetworkPlayerData)].AddSubType(100, typeof(NetworkSorPlayerData));

            // There seems to be a bug in protobuf-net,
            // if I don't do those deepclones they doesn't get registered...
            if (model.CanSerialize(typeof(InternalState)))
            {
                InternalState state = new InternalState();
                model.DeepClone(state);
                Console.WriteLine("deep cloned InternalState");
            }

            Console.WriteLine("compiling..");
            model.Compile(SerializationConstants.StateSerializerName, SerializationConstants.StateDllName);
        }
Exemplo n.º 15
0
 public static Object DeepClone(Object obj, RuntimeTypeModel model)
 {
     return(model.DeepClone(obj));
 }
Exemplo n.º 16
0
        public void WriteTo(Stream output)
        {
            var req = OriginalData == null ? new Proto.PaymentRequest() : (Proto.PaymentRequest)Serializer.DeepClone(OriginalData);

            req.pki_type = ToPKITypeString(PKIType);

            var certs = new Proto.X509Certificates();

            if (this.MerchantCertificate != null)
            {
                certs.certificate.Add(MerchantCertificate.Export(X509ContentType.Cert));
            }
            foreach (var cert in AdditionalCertificates)
            {
                certs.certificate.Add(cert.Export(X509ContentType.Cert));
            }
            MemoryStream ms = new MemoryStream();

            Serializer.Serialize(ms, certs);
            req.pki_data = ms.ToArray();
            req.serialized_payment_details = Details.ToBytes();
            req.signature = Signature;
            if (Details.Version != 1)
            {
                req.payment_details_version = Details.Version;
            }
            Serializer.Serialize(output, req);
        }
Exemplo n.º 17
0
 public T DeepClone <T>(T t)
 {
     return((T)_runtime.DeepClone(t));
 }
Exemplo n.º 18
0
 public sealed override object DeepCopyObject(object source) => s_model.DeepClone(source);
Exemplo n.º 19
0
 public static TType DeepClone <TType>(TType obj, RuntimeTypeModel model)
 {
     return((TType)model.DeepClone(obj));
 }