Skip to content

Instantly share code, notes, and snippets.

@pitermarx
Created July 29, 2021 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitermarx/80b4e58767b832564cee0170012ae93f to your computer and use it in GitHub Desktop.
Save pitermarx/80b4e58767b832564cee0170012ae93f to your computer and use it in GitHub Desktop.
A base class that implements IXunitSerializable
using System;
using System.IO;
using System.Xml.Serialization;
using Xunit.Abstractions;
public abstract class SerializableTestCase : IXunitSerializable
{
/// <inheritdoc/>
public void Deserialize(IXunitSerializationInfo info)
{
Type type = this.GetType();
using (StringReader sw = new StringReader(info.GetValue<string>("value")))
{
object value = new XmlSerializer(type).Deserialize(sw);
foreach (var prop in type.GetProperties())
{
prop.SetValue(this, prop.GetValue(value));
}
}
}
/// <inheritdoc/>
public void Serialize(IXunitSerializationInfo info)
{
Type type = this.GetType();
using (StringWriter sw = new StringWriter())
{
new XmlSerializer(type).Serialize(sw, this);
info.AddValue("value", sw.ToString(), typeof(string));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment