private static void DateTimeKind_SerializedElectively(bool includeDateTimeKind, string name) { var model = RuntimeTypeModel.Create(); model.AutoCompile = false; model.IncludeDateTimeKind = includeDateTimeKind; var original = HazTime.Create(); var clone = (HazTime)model.DeepClone(original); CompareDates(original, clone, includeDateTimeKind, "runtime"); model.CompileInPlace(); clone = (HazTime)model.DeepClone(original); CompareDates(original, clone, includeDateTimeKind, "CompileInPlace"); TypeModel compiled = model.Compile(); Assert.Equal(includeDateTimeKind, SerializeDateTimeKind(compiled)); clone = (HazTime)compiled.DeepClone(original); CompareDates(original, clone, includeDateTimeKind, "Compile"); compiled = model.Compile(name, name + ".dll"); Assert.Equal(includeDateTimeKind, SerializeDateTimeKind(compiled)); clone = (HazTime)compiled.DeepClone(original); CompareDates(original, clone, includeDateTimeKind, "Compile-dll"); PEVerify.AssertValid(name + ".dll"); }
static void Verify(TypeModel model, TimeSpan value, string expected, string expectedHaz = null) { expectedHaz ??= expected; var ms = new MemoryStream(); string Hex(bool reset = true) { var hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length); if (reset) { ms.Position = 0; ms.SetLength(0); } return(hex); } model.Serialize <TimeSpan>(ms, value); Assert.Equal(expected, Hex()); model.Serialize <TimeSpan?>(ms, value); Assert.Equal(expected, Hex()); model.Serialize(ms, (object)value); Assert.Equal(expected, Hex(reset: false)); ms.Position = 0; Assert.Equal(value, model.Deserialize <TimeSpan>(ms)); ms.Position = 0; Assert.Equal(value, model.Deserialize <TimeSpan?>(ms)); ms.Position = 0; Assert.Equal(value, model.Deserialize(ms, null, typeof(TimeSpan))); ms.Position = 0; Assert.Equal(value, model.Deserialize(ms, null, typeof(TimeSpan?))); ms.Position = 0; Assert.Equal(value, model.Deserialize <HazTime>(ms).Val); ms.Position = 0; Assert.Equal(value, ((HazTime)model.Deserialize(ms, null, typeof(HazTime))).Val); Hex(); // reset var haz = new HazTime { Val = value }; model.Serialize <HazTime>(ms, haz); Assert.Equal(expectedHaz, Hex()); model.Serialize(ms, (object)haz); Assert.Equal(expectedHaz, Hex()); }
static void CompareDates(HazTime expected, HazTime actual, bool withKind, string model) { CompareDates(expected.Utc, actual.Utc, withKind ? DateTimeKind.Utc : DateTimeKind.Unspecified, model + ":Utc"); CompareDates(expected.Local, actual.Local, withKind ? DateTimeKind.Local : DateTimeKind.Unspecified, model + ":Local"); CompareDates(expected.Unspecified, actual.Unspecified, DateTimeKind.Unspecified, model + ":Unspecified"); }