Skip to content

Instantly share code, notes, and snippets.

@jchadwick
Last active December 19, 2015 07:39
Show Gist options
  • Save jchadwick/5920602 to your computer and use it in GitHub Desktop.
Save jchadwick/5920602 to your computer and use it in GitHub Desktop.
Parser
using System;
using System.Collections.Generic;
using System.Linq;
public static class Parser
{
public static readonly IDictionary<Type, Func<string, object>> Parsers =
new Dictionary<Type, Func<string, object>>
{
{typeof (char), source => char.Parse(source)},
{typeof (decimal), source => decimal.Parse(source)},
{typeof (double), source => double.Parse(source)},
{typeof (float), source => float.Parse(source)},
{typeof (int), source => int.Parse(source)},
{typeof (uint), source => uint.Parse(source)},
{typeof (long), source => long.Parse(source)},
{typeof (ulong), source => ulong.Parse(source)},
{typeof (short), source => short.Parse(source)},
{typeof (ushort), source => ushort.Parse(source)},
{typeof (Guid), source => Guid.Parse(source)},
{typeof (DateTime), source => DateTime.Parse(source)},
{typeof (DateTime?), source =>
{
DateTime value;
if (DateTime.TryParse(source, out value))
return value;
return null;
}
},
{typeof (bool), source =>
{
if(string.IsNullOrWhiteSpace(source))
return false;
return
|| string.Equals(source, "true", StringComparison.OrdinalIgnoreCase)
|| string.Equals(source, "yes", StringComparison.OrdinalIgnoreCase)
|| string.Equals(source, "1");
}
},
};
/// <summary>
/// Parses the string to the desired type
/// </summary>
/// <param name="source">The string value.</param>
/// <returns>An object.</returns>
public static T Parse<T>(this string source)
{
var parsed = Parse(source, typeof(T));
return parsed == null ? default(T) : (T)parsed;
}
/// <summary>
/// Parses the string to the desired type
/// </summary>
/// <param name="source">The string value.</param>
/// <param name="type">The type value.</param>
/// <returns>An object.</returns>
public static object Parse(this string source, Type type)
{
if (type == typeof(string))
return source;
if (string.IsNullOrWhiteSpace(source))
return null;
if (Parsers.ContainsKey(type))
return Parsers[type](source);
if (type.IsEnum)
return Enum.Parse(type, source);
var derivedType =
Parsers.Select(x => x.Key)
.FirstOrDefault(x => x.IsAssignableFrom(type));
if (derivedType != null)
return Parsers[derivedType](source);
var underlyingType = Nullable.GetUnderlyingType(type);
if (underlyingType != null)
return Parse(source, underlyingType);
return null;
}
public static bool TryParse<T>(this string source, out object value)
{
var type = typeof(T);
if (TryParse(source, type, out value))
return true;
value = default(T);
return false;
}
public static bool TryParse(this string source, Type type, out object value)
{
try
{
value = Parse(source, type);
return true;
}
catch (Exception ex)
{
System.Diagnostics.Trace.TraceWarning(string.Format("Could not parse value '{0}' to type {1}", source, type.FullName), ex);
}
value = null;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment