public void CloneDelegateClosure() { var v = new int[] { 1, 2, 3 }; Func <int, int[]> fn = (int x) => v.Select(a => a + x).ToArray(); try { var fn2 = default(Func <int, int[]>); Serializer.Clone(fn, ref fn2, new SerializationContext()); Assert.Fail("Should have thrown while attempting to clone Func"); } catch (NotSupportedException ex) { Assert.IsTrue(ex.Message.StartsWith("Cannot clone Func")); } // register to allow IntPtr cloning in Func<int, int> and repeat - should now succeed var serializers = new KnownSerializers(); serializers.Register <Func <int, int[]> >(CloningFlags.CloneIntPtrFields); var fn3 = default(Func <int, int[]>); Serializer.Clone(fn, ref fn3, new SerializationContext(serializers)); var res = fn3(10); Assert.AreEqual(11, res[0]); Assert.AreEqual(12, res[1]); Assert.AreEqual(13, res[2]); }
public void RegisterFlags() { var serializers = new KnownSerializers(); // register different flags for multiple types serializers.Register <ClassWithIntPtr>(CloningFlags.CloneIntPtrFields); serializers.Register <ClassWithPointer>(CloningFlags.ClonePointerFields); serializers.Register <ClassWithNonSerialized>(CloningFlags.SkipNonSerializedFields); serializers.Register <StructWithAll>( CloningFlags.ClonePointerFields | CloningFlags.CloneIntPtrFields | CloningFlags.SkipNonSerializedFields); Assert.AreEqual(CloningFlags.CloneIntPtrFields, serializers.GetCloningFlags(typeof(ClassWithIntPtr))); Assert.AreEqual(CloningFlags.ClonePointerFields, serializers.GetCloningFlags(typeof(ClassWithPointer))); Assert.AreEqual(CloningFlags.SkipNonSerializedFields, serializers.GetCloningFlags(typeof(ClassWithNonSerialized))); Assert.AreEqual( CloningFlags.ClonePointerFields | CloningFlags.CloneIntPtrFields | CloningFlags.SkipNonSerializedFields, serializers.GetCloningFlags(typeof(StructWithAll))); serializers = new KnownSerializers(); // new context // registering with no flags is equivalent to None serializers.Register <ClassWithPointer>(); serializers.Register <ClassWithPointer>(CloningFlags.None); Assert.AreEqual(CloningFlags.None, serializers.GetCloningFlags(typeof(ClassWithPointer))); serializers = new KnownSerializers(); // new context // cannot re-register with different flags serializers.Register <ClassWithPointer>(CloningFlags.ClonePointerFields); Assert.ThrowsException <SerializationException>( () => serializers.Register <ClassWithPointer>(CloningFlags.SkipNonSerializedFields)); Assert.AreEqual(CloningFlags.ClonePointerFields, serializers.GetCloningFlags(typeof(ClassWithPointer))); serializers = new KnownSerializers(); // new context // once a handler has been created, cannot register flags var handler = serializers.GetHandler <ClassWithPointer>(); Assert.ThrowsException <SerializationException>( () => serializers.Register <ClassWithPointer>(CloningFlags.ClonePointerFields)); }
public void CustomSerializer() { var c = new TypeWithPolymorphicField(); c.Name = "foo"; c.Enumerable = new List <int>(); BufferWriter bw = new BufferWriter(100); var ks = new KnownSerializers(); ks.Register <TypeWithPolymorphicField, TestCustomSerializer>("some alternate name"); // include an alternate name just to exercise that code path var sc = new SerializationContext(ks); Serializer.Serialize(bw, c, sc); sc.Reset(); TypeWithPolymorphicField c2 = null; var br = new BufferReader(bw.Buffer); Serializer.Deserialize(br, ref c2, sc); Assert.AreEqual(c.Name, c2.Name); }
public void CloneNonSerializedField() { var cls = new ClassWithNonSerialized { Value = 1, Secret = 0x12345678 }; var cls2 = default(ClassWithNonSerialized); Serializer.Clone(cls, ref cls2, new SerializationContext()); Assert.AreEqual(1, cls2.Value); Assert.AreEqual(0x12345678, cls2.Secret); // register to disable cloning of NonSerialized fields - field should be skipped var serializers = new KnownSerializers(); serializers.Register <ClassWithNonSerialized>(CloningFlags.SkipNonSerializedFields); var cls3 = default(ClassWithNonSerialized); Serializer.Clone(cls, ref cls3, new SerializationContext(serializers)); Assert.AreEqual(1, cls3.Value); Assert.AreEqual(default, cls3.Secret); // skipped NonSerialized field