public void GenericCollectionMemberClonedWhenCopy()
        {
            var clone = new ObjectCloner<A>();

            var a = Helper.InitTestObject();

            clone.ForMember(V => V.Bs.Select(V0 => V0.C)).CloneMode(CloneMode.Copy);

            var aprime = clone.Clone(a);

            Assert.IsTrue(aprime.Bs.First().C != null);
        }
示例#2
0
        public void CollectionMemberClonedWhenCopy()
        {
            var clone = new ObjectCloner();

            var a = Helper.InitTestObject();

            clone.ForMember("Bs[*].C").CloneMode(CloneMode.Copy);

            var aprime = clone.Clone(a);

            Assert.IsTrue(aprime.Bs.First().C != null);
        }
示例#3
0
        public void CollectionMemberIsReference()
        {
            var clone = new ObjectCloner();
            clone.ForMember("Bs[*]").CloneMode(CloneMode.Copy);
            clone.ForMember("Bs[*].C").CloneMode(CloneMode.AsReference);

            var a = Helper.InitTestObject();
            var aprime = clone.Clone(a);

            var memberSrc = a.Bs.First();
            var memberDst = aprime.Bs.First();

            memberSrc.F1 = 1234;
            memberDst.F1 = 5678;

            Assert.AreNotEqual(memberDst.F1, memberSrc.F1);
        }
示例#4
0
        public void CollectionMemberAssignedByRef()
        {
            var clone = new ObjectCloner();

            var a = Helper.InitTestObject();

            clone.ForMember("Bs").CloneMode(CloneMode.AsReference);

            var aprime = clone.Clone(a);

            var leftMember = a.Bs.First();
            var rightMember = aprime.Bs.First();

            leftMember.F1 = 34343434;

            Assert.AreEqual(leftMember.F1, rightMember.F1);
        }
        public void GenericCollectionMemberIsReference()
        {
            var clone = new ObjectCloner<A>();
            clone.ForMember(V => V.Bs).CloneMode(CloneMode.Copy);
            clone.ForMember(V => V.Bs.Select(V0 => V0.C)).CloneMode(CloneMode.AsReference);

            var a = Helper.InitTestObject();
            var aprime = clone.Clone(a);

            var memberSrc = a.Bs.First();
            var memberDst = aprime.Bs.First();

            memberSrc.F1 = 1234;
            memberDst.F1 = 5678;

            Assert.AreNotEqual(memberDst.F1, memberSrc.F1);
        }
示例#6
0
        public void ThrowWhenReferenceDescendantSetAsCopy()
        {
            var clone = new ObjectCloner();
            clone.ForMember("Bs[*]").CloneMode(CloneMode.AsReference);
            clone.ForMember("Bs[*].C").CloneMode(CloneMode.Copy);

            var hasTrown = false;
            try
            {
                var a = Helper.InitTestObject();
                var aprime = clone.Clone(a);
            }
            catch
            {
                hasTrown = true;
            }

            Assert.IsTrue(hasTrown);
        }
示例#7
0
        public void IsReference()
        {
            var clone = new ObjectCloner();

            var a = Helper.InitTestObject();

            clone.ForMember("B.C").CloneMode(CloneMode.AsReference);

            var aprime = clone.Clone(a);

            a.B.C.F1 = 12345;

            Assert.AreEqual(a.B.C.F1, aprime.B.C.F1);
        }
示例#8
0
        public void DefaultIsCopy()
        {
            var clone = new ObjectCloner();

            var a = Helper.InitTestObject();

            var aprime = clone.Clone(a);

            a.B.F1 = 12345;
            aprime.B.F1 = 6789;

            Assert.AreNotEqual(a.B.F1, aprime.B.F1);
        }
示例#9
0
 protected virtual object GetClone()
 {
     return(ObjectCloner.Clone(this));
 }
示例#10
0
        public void MPT_Serialization()
        {
            var e1 = Get <TestUser>();

            e1.Age = 15;
            e1._mySelfReference = e1;
            TestUserExt.SetUserCode(e1, "TestUserExt_UserCode");

            Assert.AreEqual(e1.Validate().Count, 1);

            //在这里可以查看序列化后传输的字符串
            var serializedString = MobileFormatter.SerializeToString(e1);

            Assert.IsNotNull(serializedString);
            var serializedXml = MobileFormatter.SerializeToXml(e1);

            Assert.IsNotNull(serializedXml);

            #region  制对象(序列化+反序列化)

            var e2 = ObjectCloner.Clone(e1).CastTo <TestUser>();

            //实体直接定义的字段
            Assert.AreEqual(e1._ageNonserailizable, 15);
            Assert.AreEqual(e1._ageSerailizable, 15);
            Assert.AreEqual(e2._ageSerailizable, 15);
            Assert.AreEqual(e2._ageNonserailizable, 0);
            Assert.AreEqual(e2._mySelfReference, e2);
            Assert.AreEqual(e2._now, DateTime.Today);

            //Rafy属性
            Assert.IsTrue(e2.IsDirty);
            Assert.IsFalse(e2.IsNew);
            Assert.IsFalse(e2.IsDeleted);

            //一般属性
            Assert.AreEqual(e2.Age, 15);
            Assert.AreEqual(e1.Age, 15);

            //默认属性
            Assert.AreEqual(e2.Id, e1.Id);
            Assert.AreEqual(e2.Name, "DefaultName");
            Assert.AreEqual(e1.Name, "DefaultName");

            #endregion

            #region 检测具体的序列化的值

            var si = new SerializationContainerContext(new SerializationInfoContainer(0), null);
            si.IsProcessingState = true;
            e1.CastTo <IMobileObject>().SerializeState(si);

            var list = si.States.Keys.ToArray();

            Assert.IsTrue(list.Contains("Age"), "Age 是属性值,需要序列化");
            Assert.IsTrue(list.Contains("TestUserExt_UserCode"), "UserCode 是扩展属性值,需要序列化");
            Assert.IsTrue(!list.Contains("Name"), "Name 是默认值,不需要序列化");
            Assert.IsTrue(!list.Contains("Id"), "Id 是默认值,不需要序列化");

            #endregion
        }
示例#11
0
 /// <summary>
 /// Depth copy property and field of object.
 /// </summary>
 /// <returns></returns>
 public object Clone()
 {
     return(ObjectCloner.Clone(this));
 }
示例#12
0
 /// <inheritdoc/>
 public T Clone <T>(T objectToClone)
     where T : class
 {
     return(ObjectCloner.Clone(objectToClone));
 }