From 95100a8b4c66e8f155274786e35eb9d1f3118578 Mon Sep 17 00:00:00 2001 From: Peter Rekdal Sunde Date: Thu, 1 May 2014 17:20:37 +0200 Subject: [PATCH 1/5] Add support for serialization / deserialization. --- Semver.Test/SemVersionTest.cs | 19 +++++++++++++++++++ Semver/SemVersion.cs | 31 +++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) 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/SemVersion.cs b/Semver/SemVersion.cs index 7eea43b..ee77be8 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. + /// + /// + /// + /// + public 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. /// From 620e83a827d533476cc048019f535dede114ed2f Mon Sep 17 00:00:00 2001 From: Peter Rekdal Sunde Date: Thu, 1 May 2014 17:35:20 +0200 Subject: [PATCH 2/5] Add AssemblyInformationalVersion attribute --- Semver/Properties/AssemblyInfo.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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")] From cd7b3332835e2a9e4b52e6a14c0670426229a92e Mon Sep 17 00:00:00 2001 From: Peter Rekdal Sunde Date: Thu, 1 May 2014 17:35:34 +0200 Subject: [PATCH 3/5] Remove unused references --- Semver/Semver.csproj | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) 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 - - - - - - - - - + + +