using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
double _lastGoodX, _lastGoodY;
CheckCalculateNewPositions(value, _y);
CheckCalculateNewPositions(_x, value);
public void OnDeserialized(StreamingContext context)
CheckCalculateNewPositions(_x, _y);
static void CheckCalculateNewPositions(double x, double y)
if (!CalcNewPositions(x, y))
throw new ArgumentOutOfRangeException(string.Format("Values are out of bounds: ({0}, {1})", x, y));
public Foo(double x, double y)
CheckCalculateNewPositions(x, y);
static bool CalcNewPositions(double x, double y)
public static void Test()
var xml = DataContractSerializerHelper.ToContractXml(foo, settings: new XmlWriterSettings { Indent = true });
var foo2 = DataContractSerializerHelper.FromContractXml<Foo>(xml);
var xml2 = DataContractSerializerHelper.ToContractXml(foo, settings: new XmlWriterSettings { Indent = true });
Assert.That(() => DataContractSerializerHelper.FromContractXml<Foo>(GetBadXml()), Throws.Exception);
static string GetBadXml()
return @"<?xml version=""1.0"" encoding=""utf-16""?>
<Foo xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/"">
public static partial class DataContractSerializerHelper
public static string ToContractXml<T>(this T obj, DataContractSerializer serializer = null, XmlWriterSettings settings = null)
serializer = serializer ?? new DataContractSerializer(obj == null ? typeof(T) : obj.GetType());
using (var textWriter = new StringWriter())
settings = settings ?? new XmlWriterSettings { Indent = true };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
serializer.WriteObject(xmlWriter, obj);
return textWriter.ToString();
public static T FromContractXml<T>(string xml, DataContractSerializer serializer = null)
using (var textReader = new StringReader(xml ?? ""))
using (var xmlReader = XmlReader.Create(textReader))
return (T)(serializer ?? new DataContractSerializer(typeof(T))).ReadObject(xmlReader);
public static class XmlAssert
public static void AreEqual(
Assert.IsTrue(XNode.DeepEquals(Normalize(expected), Normalize(actual)));
private static XElement Normalize(XElement element)
.OrderBy(a => a.Name.ToString()),
.OrderBy(a => a.Name.ToString())
.Select(e => Normalize(e)));
.OrderBy(a => a.Name.ToString()));
return new XElement(element.Name, element.Attributes()
.OrderBy(a => a.Name.ToString()), element.Value);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];