extension method to parse a string as a given type using that type's public static Parse method if available

This commit is contained in:
Michael Becker 2020-08-24 09:22:22 -04:00
parent 9106770a8a
commit 32b2eec3d1
No known key found for this signature in database
GPG Key ID: 506F54899E2BFED7

View File

@ -20,5 +20,89 @@ namespace MBS.Framework
}
return retval;
}
/// <summary>
/// Parses the given <paramref name="value" /> with the specified <paramref name="type" />'s public static Parse(<see cref="String" />) method. If no such method exists,
/// returns <paramref name="value" />.
/// </summary>
/// <returns>The <see cref="Object" /> parsed from the given <paramref name="value" />.</returns>
/// <param name="value">The value to attempt to parse.</param>
/// <param name="type">Type.</param>
public static object Parse(this string value, Type type)
{
return Parse(value, type, value);
}
/// <summary>
/// Parses the given <paramref name="value" /> with the specified <paramref name="type" />'s public static Parse(<see cref="String" />) method. If no such method exists,
/// returns <paramref name="defaultValue" />.
/// </summary>
/// <returns>The parse.</returns>
/// <param name="value">Value.</param>
/// <param name="type">Type.</param>
/// <param name="defaultValue">Default value.</param>
public static object Parse(this string value, Type type, object defaultValue)
{
System.Reflection.MethodInfo miParse = type.GetMethod("Parse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, System.Reflection.CallingConventions.Any, new Type[] { typeof(string) }, null);
if (miParse != null)
{
// the given type implements a public static Parse(String) method, so use it
return miParse.Invoke(null, new object[] { value });
}
return defaultValue;
}
public static T Parse<T>(this string value)
{
return (T)Parse(value, typeof(T));
}
/// <summary>
/// Parses the given <paramref name="value" /> with the specified <paramref name="type" />'s public static Parse(<see cref="String" />) method. If no such method exists,
/// returns <paramref name="value" />.
/// </summary>
/// <returns>The <see cref="Object" /> parsed from the given <paramref name="value" />.</returns>
/// <param name="value">The value to attempt to parse.</param>
/// <param name="type">Type.</param>
public static bool TryParse(this string value, Type type, out object output)
{
return TryParse(value, type, value, out output);
}
/// <summary>
/// Parses the given <paramref name="value" /> with the specified <paramref name="type" />'s public static Parse(<see cref="String" />) method. If no such method exists,
/// returns <paramref name="defaultValue" />.
/// </summary>
/// <returns>The parse.</returns>
/// <param name="value">Value.</param>
/// <param name="type">Type.</param>
/// <param name="defaultValue">Default value.</param>
public static bool TryParse(this string value, Type type, object defaultValue, out object output)
{
System.Reflection.MethodInfo miParse = type.GetMethod("TryParse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, System.Reflection.CallingConventions.Any, new Type[] { typeof(string), type }, null);
if (miParse != null)
{
// the given type implements a public static Parse(String) method, so use it
object retval = null;
bool ret = (bool)miParse.Invoke(null, new object[] { value, retval });
if (ret)
{
output = retval;
return ret;
}
}
output = null;
return false;
}
public static bool TryParse<T>(this string value, out T output)
{
object retval = null;
bool ret = TryParse(value, typeof(T), out retval);
if (ret)
{
output = (T)retval;
return ret;
}
output = default(T);
return false;
}
}
}