From 32b2eec3d182e1639a0af742436cde3bc46eecbe Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 24 Aug 2020 09:22:22 -0400 Subject: [PATCH] extension method to parse a string as a given type using that type's public static Parse method if available --- MBS.Framework/StringExtensions.cs | 84 +++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/MBS.Framework/StringExtensions.cs b/MBS.Framework/StringExtensions.cs index 30fd8b3..7e26218 100644 --- a/MBS.Framework/StringExtensions.cs +++ b/MBS.Framework/StringExtensions.cs @@ -20,5 +20,89 @@ namespace MBS.Framework } return retval; } + + /// + /// Parses the given with the specified 's public static Parse() method. If no such method exists, + /// returns . + /// + /// The parsed from the given . + /// The value to attempt to parse. + /// Type. + public static object Parse(this string value, Type type) + { + return Parse(value, type, value); + } + /// + /// Parses the given with the specified 's public static Parse() method. If no such method exists, + /// returns . + /// + /// The parse. + /// Value. + /// Type. + /// Default value. + 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(this string value) + { + return (T)Parse(value, typeof(T)); + } + + + /// + /// Parses the given with the specified 's public static Parse() method. If no such method exists, + /// returns . + /// + /// The parsed from the given . + /// The value to attempt to parse. + /// Type. + public static bool TryParse(this string value, Type type, out object output) + { + return TryParse(value, type, value, out output); + } + /// + /// Parses the given with the specified 's public static Parse() method. If no such method exists, + /// returns . + /// + /// The parse. + /// Value. + /// Type. + /// Default value. + 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(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; + } } }