diff --git a/.gitignore b/.gitignore index 20b05c9..c6b8abc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.buildtools/ bin/ obj/ TestResults/ diff --git a/Semver.Test/SemVersionTest.cs b/Semver.Test/SemVersionTest.cs index dbd527e..1415435 100644 --- a/Semver.Test/SemVersionTest.cs +++ b/Semver.Test/SemVersionTest.cs @@ -1,4 +1,7 @@ using System; +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Semver.Test @@ -693,5 +696,21 @@ public void TestChangeBuild() Assert.AreEqual("alpha", v2.Prerelease); Assert.AreEqual("rel", v2.Build); } + + [TestMethod] + public void TestSerialization() + { + var semVer = new SemVersion(1, 2, 3, "alpha", "dev"); + SemVersion semVerSerializedDeserialized; + using (var ms = new MemoryStream()) + { + var bf = new BinaryFormatter(); + bf.Serialize(ms, semVer); + ms.Position = 0; + semVerSerializedDeserialized = (SemVersion) bf.Deserialize(ms); + } + Assert.AreEqual(semVer, semVerSerializedDeserialized); + } + } } diff --git a/Semver/Properties/AssemblyInfo.cs b/Semver/Properties/AssemblyInfo.cs index 4919c50..adbc1ea 100644 --- a/Semver/Properties/AssemblyInfo.cs +++ b/Semver/Properties/AssemblyInfo.cs @@ -7,5 +7,6 @@ [assembly: AssemblyCopyright("Copyright © 2013 Max Hauser")] [assembly: ComVisible(false)] [assembly: Guid("e208ca67-5b59-45d9-a29a-7f30137d3beb")] -[assembly: AssemblyVersion("1.0.0")] -[assembly: AssemblyFileVersion("1.0.0")] +[assembly: AssemblyVersion("1.1.2")] +[assembly: AssemblyFileVersion("1.1.2")] +[assembly: AssemblyInformationalVersion("1.1.2")] diff --git a/Semver/SemVersion.cs b/Semver/SemVersion.cs index 7eea43b..0f26634 100644 --- a/Semver/SemVersion.cs +++ b/Semver/SemVersion.cs @@ -1,5 +1,7 @@ using System; using System.Globalization; +using System.Runtime.Serialization; +using System.Security.Permissions; using System.Text.RegularExpressions; namespace Semver @@ -8,7 +10,8 @@ namespace Semver /// A semantic version implementation. /// Conforms to v2.0.0 of http://semver.org/ /// - public sealed class SemVersion : IComparable, IComparable + [Serializable] + public sealed class SemVersion : IComparable, IComparable, ISerializable { static Regex parseEx = new Regex(@"^(?\d+)" + @@ -18,6 +21,23 @@ public sealed class SemVersion : IComparable, IComparable @"(\+(?[0-9A-Za-z\-\.]+))?$", RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.ExplicitCapture); + /// + /// Initializes a new instance of the class. + /// + /// + /// + /// + private SemVersion(SerializationInfo info, StreamingContext context) + { + if (info == null) throw new ArgumentNullException("info"); + var semVersion = Parse(info.GetString("SemVersion")); + Major = semVersion.Major; + Minor = semVersion.Minor; + Patch = semVersion.Patch; + Prerelease = semVersion.Prerelease; + Build = semVersion.Build; + } + /// /// Initializes a new instance of the class. /// @@ -117,7 +137,7 @@ public static bool TryParse(string version, out SemVersion semver, bool strict = semver = Parse(version, strict); return true; } - catch(Exception) + catch (Exception) { semver = null; return false; @@ -391,6 +411,13 @@ public override int GetHashCode() } } + [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + if (info == null) throw new ArgumentNullException("info"); + info.AddValue("SemVersion", ToString()); + } + /// /// Implicit conversion from string to SemVersion. /// diff --git a/Semver/Semver.csproj b/Semver/Semver.csproj index 5737d7a..89b1af0 100644 --- a/Semver/Semver.csproj +++ b/Semver/Semver.csproj @@ -33,19 +33,13 @@ 4 bin\Release\Semver.XML - - - - - - - - - + + +