Created
December 2, 2014 21:55
-
-
Save greggman/875a4f0a8f826bc0bfdd to your computer and use it in GitHub Desktop.
How to serialize primitives in a generic way
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
// ------------------------------------------------------------------------------ | |
// <autogenerated> | |
// This code was generated by a tool. | |
// Mono Runtime Version: 4.0.30319.1 | |
// | |
// Changes to this file may cause incorrect behavior and will be lost if | |
// the code is regenerated. | |
// </autogenerated> | |
// ------------------------------------------------------------------------------ | |
using System; | |
using NUnit.Framework; | |
namespace testit | |
{ | |
[TestFixture()] | |
public class Test | |
{ | |
class Primitives { | |
public Boolean someBoolean = true; | |
public Byte someByte = 1; | |
public SByte someSByte = -1; | |
public Int16 someInt16 = -2; | |
public UInt16 someUInt16 = 2; | |
public Int32 someInt32 = -3; | |
public UInt32 someUInt32 = 3; | |
public Int64 someInt64 = -4; | |
public UInt64 someUInt64 = 4; | |
public Char someChar = 'a'; | |
public Double someDouble = 1.23; | |
public Single someSingle = 2.34f; | |
public float somefloat = 3.45f; | |
public int someint = 5; | |
public bool somebool = true; | |
}; | |
[Test()] | |
public void TestCase () | |
{ | |
Primitives p = new Primitives(); | |
p.someBoolean = false; | |
p.someByte = 6; | |
p.someSByte = -6; | |
p.someInt16 = -7; | |
p.someUInt16 = 7; | |
p.someInt32 = -8; | |
p.someUInt32 = 8; | |
p.someInt64 = -9; | |
p.someUInt64 = 9; | |
p.someChar = 'b'; | |
p.someDouble = 5.6; | |
p.someSingle = 6.7f; | |
p.somefloat = 7.8f; | |
p.someint = 12; | |
p.somebool = false; | |
Primitives p2 = new Primitives(); | |
System.Reflection.FieldInfo[] fields = p.GetType().GetFields(); | |
foreach (System.Reflection.FieldInfo info in fields) { | |
object fieldValue = info.GetValue(p); | |
if (fieldValue != null) { | |
System.Type type = fieldValue.GetType(); | |
string s = System.ComponentModel.TypeDescriptor.GetConverter(type).ConvertToInvariantString(fieldValue); | |
object o = System.ComponentModel.TypeDescriptor.GetConverter(type).ConvertFromInvariantString(s); | |
Assert.AreEqual(fieldValue, o); | |
info.SetValue(p2, o); | |
} | |
} | |
Assert.AreEqual(p.someBoolean, p2.someBoolean); | |
Assert.AreEqual(p.someByte, p2.someByte); | |
Assert.AreEqual(p.someSByte, p2.someSByte); | |
Assert.AreEqual(p.someInt16, p2.someInt16); | |
Assert.AreEqual(p.someUInt16, p2.someUInt16); | |
Assert.AreEqual(p.someInt32, p2.someInt32); | |
Assert.AreEqual(p.someUInt32, p2.someUInt32); | |
Assert.AreEqual(p.someInt64, p2.someInt64); | |
Assert.AreEqual(p.someUInt64, p2.someUInt64); | |
Assert.AreEqual(p.someChar, p2.someChar); | |
Assert.AreEqual(p.someDouble, p2.someDouble); | |
Assert.AreEqual(p.someSingle, p2.someSingle); | |
Assert.AreEqual(p.somefloat, p2.somefloat); | |
Assert.AreEqual(p.someint, p2.someint); | |
Assert.AreEqual(p.somebool, p2.somebool); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment