roadie/RoadieLibrary/Utility/SafeParser.cs

193 lines
5 KiB
C#
Raw Normal View History

2018-11-03 21:21:36 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace Roadie.Library.Utility
{
public static class SafeParser
{
2018-11-04 20:33:37 +00:00
public static T ChangeType<T>(object value)
{
var t = typeof(T);
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return default(T);
}
t = Nullable.GetUnderlyingType(t);
}
return (T)Convert.ChangeType(value, t);
}
2018-11-03 21:21:36 +00:00
/// <summary>
2018-11-04 20:33:37 +00:00
/// Return a value that is safe to use as a Key value
2018-11-03 21:21:36 +00:00
/// </summary>
2018-11-04 20:33:37 +00:00
public static string KeyFriendly(string input)
{
return input;
}
/// <summary>
/// Safely return a Boolean for a given Input.
/// <remarks>Has Additional String Operations</remarks>
/// </summary>
public static bool ToBoolean(object input)
2018-11-03 21:21:36 +00:00
{
if (input == null)
{
2018-11-04 20:33:37 +00:00
return false;
2018-11-03 21:21:36 +00:00
}
2018-11-04 20:33:37 +00:00
var t = input as bool?;
if (t != null)
2018-11-03 21:21:36 +00:00
{
2018-11-04 20:33:37 +00:00
return t.Value;
2018-11-03 21:21:36 +00:00
}
2018-11-04 20:33:37 +00:00
switch (input.ToString().ToLower())
2018-11-03 21:21:36 +00:00
{
2018-11-04 20:33:37 +00:00
case "true":
case "1":
case "y":
case "yes":
return true;
2018-11-03 21:21:36 +00:00
2018-11-04 20:33:37 +00:00
default:
return false;
2018-11-03 21:21:36 +00:00
}
}
2018-11-20 04:47:12 +00:00
public static Guid? ToGuid(object input)
{
if(input == null)
{
return null;
}
2018-11-22 13:48:32 +00:00
var i = input.ToString();
if(i[1] == ':')
{
i = i.Substring(2, i.Length - 2);
}
if (!Guid.TryParse(i.ToString(), out Guid result))
2018-11-20 04:47:12 +00:00
{
return null;
}
return result;
}
2018-11-03 21:21:36 +00:00
public static DateTime? ToDateTime(object input)
{
if (input == null)
{
return default(DateTime?);
}
try
{
DateTime dt = DateTime.MinValue;
var i = input as string ?? input.ToString();
if (!string.IsNullOrEmpty(i))
{
i.Replace("-", "/");
var parts = i.Contains("/") ? i.Split('/').ToList() : new List<string> { i };
while (parts.Count() < 3)
{
parts.Insert(0, "01");
}
var tsRaw = string.Empty;
foreach (var part in parts)
{
if (tsRaw.Length > 0)
{
tsRaw += "/";
}
tsRaw += part;
}
DateTime.TryParse(tsRaw, out dt);
}
try
{
return ChangeType<DateTime?>(input);
}
catch
{
}
return dt != DateTime.MinValue ? (DateTime?)dt : null;
}
catch
{
return default(DateTime?);
}
}
2018-11-04 20:33:37 +00:00
public static T ToEnum<T>(object input) where T : struct, IConvertible
2018-11-03 21:21:36 +00:00
{
if (input == null)
{
2018-11-04 20:33:37 +00:00
return default(T);
2018-11-03 21:21:36 +00:00
}
2018-11-04 20:33:37 +00:00
Enum.TryParse(input.ToString(), out T r);
return r;
2018-11-03 21:21:36 +00:00
}
/// <summary>
2018-11-04 20:33:37 +00:00
/// Safely Return a Number For Given Input
2018-11-03 21:21:36 +00:00
/// </summary>
2018-11-04 20:33:37 +00:00
public static T ToNumber<T>(object input)
2018-11-03 21:21:36 +00:00
{
if (input == null)
{
2018-11-04 20:33:37 +00:00
return default(T);
2018-11-03 21:21:36 +00:00
}
2018-11-04 20:33:37 +00:00
try
2018-11-03 21:21:36 +00:00
{
2018-11-04 20:33:37 +00:00
return ChangeType<T>(input);
2018-11-03 21:21:36 +00:00
}
2018-11-04 20:33:37 +00:00
catch
2018-11-03 21:21:36 +00:00
{
2018-11-04 20:33:37 +00:00
return default(T);
2018-11-03 21:21:36 +00:00
}
}
2018-11-04 20:33:37 +00:00
public static string ToString(object input, string defaultValue = null)
2018-11-03 21:21:36 +00:00
{
2018-11-04 20:33:37 +00:00
defaultValue = defaultValue ?? string.Empty;
if (input == null)
{
return defaultValue;
}
var r = input as string;
if (r != null)
{
return r.Trim();
}
return defaultValue;
2018-11-03 21:21:36 +00:00
}
public static int? ToYear(string input)
{
if (string.IsNullOrEmpty(input))
{
return null;
}
int parsed = -1;
if (input.Length == 4)
{
if (int.TryParse(input, out parsed))
{
return parsed > 0 ? (int?)parsed : null;
}
}
else if (input.Length > 4)
{
if (int.TryParse(input.Substring(0, 4), out parsed))
{
return parsed > 0 ? (int?)parsed : null;
}
}
return null;
}
}
2018-11-04 20:33:37 +00:00
}