cleanup.
This commit is contained in:
parent
304a984262
commit
80b616b914
@ -1,58 +0,0 @@
|
||||
using PoweredSoft.Types.Converters;
|
||||
using PoweredSoft.Types.Interface;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
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);
|
||||
|
||||
if (notNullType != null)
|
||||
ret = Activator.CreateInstance(type, new object[] { ret });
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user