Add project files.

This commit is contained in:
David Lebée 2018-04-17 21:16:22 -05:00
parent 8e03e50198
commit 96cfb46de2
10 changed files with 306 additions and 0 deletions

View File

@ -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);
}
}
}

View File

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7CB0B9D3-8B0C-4A3E-9A1B-8DB4A8C4646E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PoweredSoft.Types.Test</RootNamespace>
<AssemblyName>PoweredSoft.Types.Test</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConvertionTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PoweredSoft.Types\PoweredSoft.Types.csproj">
<Project>{ce94724d-4785-47fb-a04d-65b55a515248}</Project>
<Name>PoweredSoft.Types</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets')" />
</Project>

View File

@ -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")]

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="1.2.0" targetFramework="net462" />
<package id="MSTest.TestFramework" version="1.2.0" targetFramework="net462" />
</packages>

31
PoweredSoft.Types.sln Normal file
View File

@ -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

View File

@ -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<ITypeConverter> Converters { get; internal set; } = new List<ITypeConverter>
{
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;
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Authors>David Lebee</Authors>
<Company>Powered Softwares Inc</Company>
<Description>Allows to easily convert types.</Description>
<RepositoryUrl>https://github.com/PoweredSoft/Types</RepositoryUrl>
<PackageProjectUrl>https://github.com/PoweredSoft/Types</PackageProjectUrl>
<RepositoryType>github</RepositoryType>
<PackageTags>types conversion</PackageTags>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>
</Project>