using System; using System.Collections.Generic; namespace MBS.Framework { public static class StringExtensions { public static string Capitalize(this string value) { if (String.IsNullOrEmpty(value)) return value; if (value.Length == 1) return value.ToUpper(); return value[0].ToString().ToUpper() + value.Substring(1); } public static string ReplaceVariables(this string value, IEnumerable> dict) { string retval = value; foreach (KeyValuePair kvp in dict) { retval = retval.Replace("$(" + kvp.Key + ")", kvp.Value == null ? String.Empty : kvp.Value.ToString()); } 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)); } public static T TryParse(this string value, T defaultValue = default(T)) { if (TryParse(value, typeof(T), out object output)) return (T)output; return defaultValue; } /// /// 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, new Type[] { typeof(string), type.MakeByRefType() }, null); if (miParse != null) { // the given type implements a public static Parse(String) method, so use it object retval = null; object[] parms = new object[] { value, retval }; bool ret = (bool)miParse.Invoke(null, parms); retval = parms[1]; 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; } } }