diff --git a/PoweredSoft.Types.Test/ConvertionTests.cs b/PoweredSoft.Types.Test/ConvertionTests.cs new file mode 100644 index 0000000..0766de7 --- /dev/null +++ b/PoweredSoft.Types.Test/ConvertionTests.cs @@ -0,0 +1,67 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace PoweredSoft.Types.Test +{ + [TestClass] + public class ConvertionTests + { + [TestMethod] + public void StringToGuid() + { + var guid = Guid.NewGuid(); + var guidStr = guid.ToString(); + var guid2 = guidStr.To(typeof(Guid)); + Assert.AreEqual(guid, guid2); + } + + [TestMethod] + public void StringToDateTime() + { + var date = DateTime.Now; + var dateStr = date.ToString(); + var date2 = (DateTime)dateStr.To(typeof(DateTime)); + Assert.IsTrue(date.Year == date2.Year && date.Month == date2.Month && date.Day == date2.Day); + Assert.IsTrue(date.Hour == date2.Hour && date.Minute == date2.Minute && date.Second == date2.Second); + } + + [TestMethod] + public void StringToGuidNullable() + { + Guid? guid = Guid.NewGuid(); + var guidStr = guid.ToString(); + var guid2 = guidStr.To(typeof(Guid?)); + Assert.AreEqual(guid, guid2); + } + + [TestMethod] + public void StringToDateNullable() + { + DateTime? date = DateTime.Now; + var dateStr = date.ToString(); + var date2 = (DateTime?)dateStr.To(typeof(DateTime?)); + Assert.IsTrue(date.Value.Year == date2.Value.Year && date.Value.Month == date2.Value.Month && date.Value.Day == date2.Value.Day); + Assert.IsTrue(date.Value.Hour == date2.Value.Hour && date.Value.Minute == date2.Value.Minute && date.Value.Second == date2.Value.Second); + } + + [TestMethod] + public void IntToStr() + { + int a = 123; + var b = a.To(typeof(string)); + Assert.AreEqual("123", b); + } + + [TestMethod] + public void StrToInt() + { + var a = "123"; + var b = a.To(typeof(int)); + Assert.AreEqual(123, b); + + var a1 = "12345"; + var b1 = a1.To(typeof(int?)); + Assert.AreEqual(b1, 12345); + } + } +} diff --git a/PoweredSoft.Types.Test/PoweredSoft.Types.Test.csproj b/PoweredSoft.Types.Test/PoweredSoft.Types.Test.csproj new file mode 100644 index 0000000..63daccc --- /dev/null +++ b/PoweredSoft.Types.Test/PoweredSoft.Types.Test.csproj @@ -0,0 +1,73 @@ + + + + + Debug + AnyCPU + {7CB0B9D3-8B0C-4A3E-9A1B-8DB4A8C4646E} + Library + Properties + PoweredSoft.Types.Test + PoweredSoft.Types.Test + v4.6.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + {ce94724d-4785-47fb-a04d-65b55a515248} + PoweredSoft.Types + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/PoweredSoft.Types.Test/Properties/AssemblyInfo.cs b/PoweredSoft.Types.Test/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2131831 --- /dev/null +++ b/PoweredSoft.Types.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("PoweredSoft.Types.Test")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PoweredSoft.Types.Test")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("7cb0b9d3-8b0c-4a3e-9a1b-8db4a8c4646e")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/PoweredSoft.Types.Test/packages.config b/PoweredSoft.Types.Test/packages.config new file mode 100644 index 0000000..d57d157 --- /dev/null +++ b/PoweredSoft.Types.Test/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/PoweredSoft.Types.sln b/PoweredSoft.Types.sln new file mode 100644 index 0000000..c451779 --- /dev/null +++ b/PoweredSoft.Types.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27130.2036 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PoweredSoft.Types", "PoweredSoft.Types\PoweredSoft.Types.csproj", "{CE94724D-4785-47FB-A04D-65B55A515248}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.Types.Test", "PoweredSoft.Types.Test\PoweredSoft.Types.Test.csproj", "{7CB0B9D3-8B0C-4A3E-9A1B-8DB4A8C4646E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CE94724D-4785-47FB-A04D-65B55A515248}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CE94724D-4785-47FB-A04D-65B55A515248}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CE94724D-4785-47FB-A04D-65B55A515248}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CE94724D-4785-47FB-A04D-65B55A515248}.Release|Any CPU.Build.0 = Release|Any CPU + {7CB0B9D3-8B0C-4A3E-9A1B-8DB4A8C4646E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7CB0B9D3-8B0C-4A3E-9A1B-8DB4A8C4646E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7CB0B9D3-8B0C-4A3E-9A1B-8DB4A8C4646E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7CB0B9D3-8B0C-4A3E-9A1B-8DB4A8C4646E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7917801F-1B2C-4C10-BF95-646FD2C8DAE3} + EndGlobalSection +EndGlobal diff --git a/PoweredSoft.Types/Convert.cs b/PoweredSoft.Types/Convert.cs new file mode 100644 index 0000000..c542da5 --- /dev/null +++ b/PoweredSoft.Types/Convert.cs @@ -0,0 +1,56 @@ +using PoweredSoft.Types.Converters; +using PoweredSoft.Types.Interface; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace PoweredSoft.Types +{ + public static class Converter + { + public static List Converters { get; internal set; } = new List + { + new StringToDateConverter(), + new StringToGuidConverter() + }; + + public static void RegisterConverter(ITypeConverter converter) + { + lock (Converters) + { + Converters.Add(converter); + } + } + + public static void ReplaceConverter(ITypeConverter converter, Type source, Type destination) + { + lock (Converters) + { + Converters.RemoveAll(t => t.CanConvert(source, destination)); + Converters.Add(converter); + } + } + + public static object To(this object source, Type type) + { + object ret = null; + + // safe if null. + if (source == null) + return ret; + + // establish final type. + var notNullType = Nullable.GetUnderlyingType(type); + var finalType = notNullType ?? type; + var converter = Converters.FirstOrDefault(t => t.CanConvert(source.GetType(), finalType)); + if (converter != null) + ret = converter.Convert(source, finalType); + else + ret = Convert.ChangeType(source, finalType); + + return ret; + } + } +} diff --git a/PoweredSoft.Types/Converters/StringToDateConverter.cs b/PoweredSoft.Types/Converters/StringToDateConverter.cs new file mode 100644 index 0000000..06b4fb8 --- /dev/null +++ b/PoweredSoft.Types/Converters/StringToDateConverter.cs @@ -0,0 +1,13 @@ +using PoweredSoft.Types.Interface; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.Types.Converters +{ + public class StringToDateConverter : ITypeConverter + { + public bool CanConvert(Type source, Type destination) => source == typeof(string) && destination == typeof(DateTime); + public object Convert(object source, Type destination) => DateTime.Parse((string)source); + } +} diff --git a/PoweredSoft.Types/Converters/StringToGuidConverter.cs b/PoweredSoft.Types/Converters/StringToGuidConverter.cs new file mode 100644 index 0000000..ecd260d --- /dev/null +++ b/PoweredSoft.Types/Converters/StringToGuidConverter.cs @@ -0,0 +1,13 @@ +using PoweredSoft.Types.Interface; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.Types.Converters +{ + public class StringToGuidConverter : ITypeConverter + { + public bool CanConvert(Type source, Type destination) => source == typeof(string) && destination == typeof(Guid); + public object Convert(object source, Type destination) => Guid.Parse((string)source); + } +} diff --git a/PoweredSoft.Types/Interface/ITypeConverter.cs b/PoweredSoft.Types/Interface/ITypeConverter.cs new file mode 100644 index 0000000..c9a8b7a --- /dev/null +++ b/PoweredSoft.Types/Interface/ITypeConverter.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.Types.Interface +{ + public interface ITypeConverter + { + bool CanConvert(Type source, Type destination); + object Convert(object source, Type destination); + } +} diff --git a/PoweredSoft.Types/PoweredSoft.Types.csproj b/PoweredSoft.Types/PoweredSoft.Types.csproj new file mode 100644 index 0000000..d3e2bfa --- /dev/null +++ b/PoweredSoft.Types/PoweredSoft.Types.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.0 + false + David Lebee + Powered Softwares Inc + Allows to easily convert types. + https://github.com/PoweredSoft/Types + https://github.com/PoweredSoft/Types + github + types conversion + false + + +