-
-
Save jsmarsch/d0dcade8c656b94f5c1c to your computer and use it in GitHub Desktop.
Example of using serialization to copy a struct
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
// this is the unit test (using MSTest in VS 2013). | |
// put in in a separate Unit Test project in the same solution, and set a reference to the | |
// project that contains the other 2 files. | |
using System; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using VectorCopyDemo; | |
namespace TestStructureCopy | |
{ | |
[TestClass] | |
public class TestStructureCopy | |
{ | |
[TestMethod] | |
public void DeserializedStructShouldMatchOriginal() | |
{ | |
// arrange | |
Vector3DF[][] testSet = GetTestVectors(); | |
var copier = new VectorSerializer(); | |
//act | |
byte[] bytes = copier.SerializeVectors(testSet); | |
Vector3DF[][] copiedVectors = copier.DeserializeVectors(bytes); | |
//assert | |
Assert.AreEqual(testSet.Length, copiedVectors.Length ); | |
for (int outer = 0; outer < testSet.GetLength(0); outer++) | |
{ | |
Vector3DF[] original = testSet[outer]; | |
Vector3DF[] copied = copiedVectors[outer]; | |
Assert.IsTrue(original.SequenceEqual(copied)); | |
} | |
} | |
private Vector3DF[][] GetTestVectors() | |
{ | |
var testList = new Vector3DF[][] | |
{ | |
new Vector3DF[] | |
{ | |
new Vector3DF(1, 2, 3), | |
new Vector3DF(4, 5, 6) | |
}, | |
new Vector3DF[] | |
{ | |
new Vector3DF(2, 3, 4) | |
} | |
}; | |
return testList; | |
} | |
} | |
} |
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
// This is my stub version of your Vector3DF struct | |
using System; | |
namespace VectorCopyDemo | |
{ | |
[Serializable] | |
public struct Vector3DF | |
{ | |
public readonly float X; | |
public readonly float Y; | |
public readonly float Z; | |
public Vector3DF(float x, float y, float z) | |
: this() | |
{ | |
X = x; | |
Y = y; | |
Z = z; | |
} | |
public bool Equals(Vector3DF other) | |
{ | |
return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
return obj is Vector3DF && Equals((Vector3DF) obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
int hashCode = X.GetHashCode(); | |
hashCode = (hashCode * 397) ^ Y.GetHashCode(); | |
hashCode = (hashCode * 397) ^ Z.GetHashCode(); | |
return hashCode; | |
} | |
} | |
} | |
} |
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.IO; | |
using System.Runtime.Serialization.Formatters.Binary; | |
namespace VectorCopyDemo | |
{ | |
public class VectorSerializer | |
{ | |
public byte[] SerializeVectors(Vector3DF[][] vectors) | |
{ | |
var formatter = new BinaryFormatter(); | |
using (var stream = new MemoryStream()) | |
{ | |
formatter.Serialize(stream, vectors); | |
return stream.ToArray(); | |
} | |
} | |
public Vector3DF[][] DeserializeVectors(byte[] vectorBuffer) | |
{ | |
var formatter = new BinaryFormatter(); | |
using (var stream = new MemoryStream(vectorBuffer, false)) | |
{ | |
return (Vector3DF[][])formatter.Deserialize(stream); | |
} | |
} | |
} | |
} |
Thanks man! Glad it worked out for you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi JMarsch, this is Calorified from stackoverflow, Thought to brighten up your afternoon! You are one heck of a cracker! Your code works for me. Except that my Vector3DF is not so much an array of arrays like you said. The GetSlices function is meant to separate each vector collection in each frame that is tracked.
Now, I need to find the norm of each vector. Cheese and Cheddar! Thanks again!