Created
July 29, 2021 15:42
-
-
Save pitermarx/80b4e58767b832564cee0170012ae93f to your computer and use it in GitHub Desktop.
A base class that implements IXunitSerializable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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