/// <summary> /// String to bool /// </summary> /// <param name="str"></param> /// <param name="ret"></param> /// <returns></returns> public static bool StringToBool(string str, out Ret ret) { bool b = false; try { b = bool.Parse(str); } catch (Exception e) { ret = new Ret(LogLevel.Error, 1, "String:" + str + " with illegal format for bool\n" + e.Message); } ret = Ret.ok; return(b); }
/// <summary> /// String to double /// </summary> /// <param name="str"></param> /// <param name="ret"></param> /// <returns></returns> public static double StringToDouble(string str, out Ret ret) { double d = 0; try { d = double.Parse(str); } catch (Exception e) { ret = new Ret(LogLevel.Error, 1, "String:" + str + " with illegal format for double\n" + e.Message); } ret = Ret.ok; return(d); }
/// <summary> /// String to float /// </summary> /// <param name="str"></param> /// <param name="ret"></param> /// <returns></returns> public static float StringToFloat(string str, out Ret ret) { float f = 0; try { f = float.Parse(str); } catch (Exception e) { ret = new Ret(LogLevel.Error, 1, "String:" + str + " with illegal format for float\n" + e.Message); } ret = Ret.ok; return(f); }
/// <summary> /// String to ulong /// </summary> /// <param name="str"></param> /// <param name="ret"></param> /// <returns></returns> public static ulong StringToUlong(string str, out Ret ret) { ulong i = 0; try { i = ulong.Parse(str); } catch (Exception e) { ret = new Ret(LogLevel.Error, 1, "String:" + str + " with illegal format for ulong\n" + e.Message); } ret = Ret.ok; return(i); }
/// <summary> /// String to short /// </summary> /// <param name="str"></param> /// <param name="ret"></param> /// <returns></returns> public static short StringToShort(string str, out Ret ret) { short i = 0; try { i = short.Parse(str); } catch (Exception e) { ret = new Ret(LogLevel.Error, 1, "String:" + str + " with illegal format for short\n" + e.Message); } ret = Ret.ok; return(i); }
/// <summary> /// String to byte /// </summary> /// <param name="str"></param> /// <param name="ret"></param> /// <returns></returns> public static byte StringToByte(string str, out Ret ret) { byte i = 0; try { i = byte.Parse(str); } catch (Exception e) { ret = new Ret(LogLevel.Error, 1, "String:" + str + " with illegal format for byte\n" + e.Message); } ret = Ret.ok; return(i); }
/// <summary> /// Json string to object /// </summary> /// <returns>The to object.</returns> /// <param name="jsonStr">Json string.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T JsonToObject <T>(string json, out Ret ret) { T obj; try { obj = JsonConvert.DeserializeObject <T>(json); } catch (Exception e) { ret = new Ret(LogLevel.Error, 1, "Json:" + json + " with illegal format\n" + e.Message); return(default(T)); } ret = Ret.ok; return(obj); }
/// <summary> /// Json appended /// </summary> /// <param name="json"></param> /// <param name="kvs"></param> /// <returns></returns> public static string JsonAppend(string json, out Ret ret, params KV <string, object>[] kvs) { Dictionary <string, object> table = JsonToTable(json); if (table == null) { ret = new Ret(LogLevel.Error, 1, "Json:" + json + " with illegal format"); return(""); } foreach (KV <string, object> kv in kvs) { table.Add(kv.key, kv.val); } ret = Ret.ok; return(ObjectToJson(table)); }
/// <summary> /// Get IPv4 of host /// </summary> /// <param name="ret"></param> /// <returns></returns> public static string GetLocalIP(out Ret ret) { try { string name = Dns.GetHostName(); IPHostEntry entry = Dns.GetHostEntry(name); foreach (IPAddress ipa in entry.AddressList) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { ret = Ret.ok; return(ipa.ToString()); } } ret = new Ret(LogLevel.Warning, 1, "No IPv4 by host name: " + name); return(""); } catch (Exception e) { ret = new Ret(LogLevel.Error, 2, e.Message); return(""); } }