private object ConvertFromString(string strValue) { try { //convert to property real type if (PropertyType == typeof(string)) { return(strValue); } else if (PropertyType == typeof(TimeSpan)) { return(TikTimeHelper.FromTikTimeToTimeSpan(strValue)); } else if (PropertyType == typeof(int)) { return(int.Parse(strValue)); } else if (PropertyType == typeof(long)) { return(long.Parse(strValue)); } else if (PropertyType == typeof(double)) { return(double.Parse(strValue)); } else if (PropertyType == typeof(float)) { return(float.Parse(strValue)); } else if (PropertyType == typeof(bool)) { return(string.Equals(strValue, "true", StringComparison.OrdinalIgnoreCase) || string.Equals(strValue, "yes", StringComparison.OrdinalIgnoreCase)); } #if NET20 || NET35 || NET40 else if (PropertyType.IsEnum) { return Enum.GetNames(PropertyType) .Where(en => string.Equals(PropertyType.GetMember(en)[0].GetCustomAttribute <TikEnumAttribute>(false).Value, strValue, StringComparison.OrdinalIgnoreCase)) #else else if (PropertyType.GetTypeInfo().IsEnum) return(Enum.GetNames(PropertyType) .Where(en => string.Equals(PropertyType.GetRuntimeField(en).GetCustomAttribute <TikEnumAttribute>(false).Value, strValue, StringComparison.OrdinalIgnoreCase)) #endif .Select(en => Enum.Parse(PropertyType, en, true)) .Single()); //TODO safer implementation } //else if (PropertyType == typeof(Ipv4Address)) // return new Ipv4Address(strValue); //else if (PropertyType == typeof(Ipv4AddressWithSubnet)) // return new Ipv4AddressWithSubnet(strValue); //else if (PropertyType == typeof(MacAddress)) // return new MacAddress(strValue); else { throw new NotImplementedException(string.Format("Property type {0} not supported.", PropertyType)); } }
private object ConvertFromString(string strValue) { try { //convert to property real type if (PropertyType == typeof(string)) { return(strValue); } else if (PropertyType == typeof(TimeSpan)) { return(TikTimeHelper.FromTikTimeToTimeSpan(strValue)); } else if (PropertyType == typeof(int)) { return(int.Parse(strValue)); } else if (PropertyType == typeof(long)) { return(long.Parse(strValue)); } else if (PropertyType == typeof(byte)) { return(byte.Parse(strValue)); } else if (PropertyType == typeof(bool)) { return(string.Equals(strValue, "true", StringComparison.OrdinalIgnoreCase) || string.Equals(strValue, "yes", StringComparison.OrdinalIgnoreCase)); } else if (PropertyType.GetTypeInfo().IsEnum) { return(Enum.GetNames(PropertyType) .Where(en => string.Equals(PropertyType.GetRuntimeField(en).GetCustomAttribute <TikEnumAttribute>(false).Value, strValue, StringComparison.OrdinalIgnoreCase)) .Select(en => Enum.Parse(PropertyType, en, true)) .Single()); //TODO safer implementation } //else if (PropertyType == typeof(Ipv4Address)) // return new Ipv4Address(strValue); //else if (PropertyType == typeof(Ipv4AddressWithSubnet)) // return new Ipv4AddressWithSubnet(strValue); //else if (PropertyType == typeof(MacAddress)) // return new MacAddress(strValue); else { throw new NotImplementedException(string.Format("Property type {0} not supported.", PropertyType)); } } catch (NotImplementedException) { throw; } catch (Exception ex) { throw new FormatException(string.Format("Value '{0}' for property '{1}({2})' is not in expected format '{3}'.", strValue, PropertyName, FieldName, PropertyType), ex); } }
private string ConvertToString(object propValue) { if (propValue is string) { return((string)propValue); } //convert to string used in mikrotik if (PropertyType == typeof(string)) { return(propValue.ToString()); } else if (PropertyType == typeof(TimeSpan)) { return(TikTimeHelper.ToTikTime((int)((TimeSpan)propValue).TotalSeconds)); } else if (PropertyType == typeof(int)) { return(((int)propValue).ToString()); } else if (PropertyType == typeof(long)) { return(((long)propValue).ToString()); } else if (PropertyType == typeof(bool)) { return(((bool)propValue) ? "yes" : "no"); //TODO add attribute definition for support true/false } else if (PropertyType.IsEnum) { return(PropertyType.GetMember(propValue.ToString())[0].GetCustomAttribute <TikEnumAttribute>(false).Value); //TODO safer implementation } //else if (PropertyType == typeof(Ipv4Address)) // return ((Ipv4Address)propValue).Address; //else if (PropertyType == typeof(Ipv4AddressWithSubnet)) // return ((Ipv4AddressWithSubnet)propValue).Address; //else if (PropertyType == typeof(MacAddress)) // return ((MacAddress)propValue).Address; else { throw new NotImplementedException(string.Format("Property type {0} not supported.", PropertyType)); } }
public void ToTestMethod_Day() { Assert.AreEqual((3600 * 24 * 2), TikTimeHelper.FromTikTime("2d")); }
public void ToTestMethod_Hour() { Assert.AreEqual(3600, TikTimeHelper.FromTikTime("1h")); }
public void ToTestMethod_Minute() { Assert.AreEqual(60, TikTimeHelper.FromTikTime("1m")); }
public void ToTestMethod_Second() { Assert.AreEqual(10, TikTimeHelper.FromTikTime("10s")); }
public void FromTestMethod_Over1Year() { Assert.AreEqual("71w3d", TikTimeHelper.ToTikTime((3600 * 24 * 500))); }
public void ToTestMethod_AllFields() { Assert.AreEqual((1 + 120 + 3600 + 3600 * 24 * 10), TikTimeHelper.FromTikTime("1w3d1h2m1s")); }
public void FromTestMethod_Day() { Assert.AreEqual("2d", TikTimeHelper.ToTikTime((3600 * 24 * 2))); }
public void FromTestMethod_Hour() { Assert.AreEqual("1h", TikTimeHelper.ToTikTime(3600)); }
public void FromTestMethod_Minute() { Assert.AreEqual("1m", TikTimeHelper.ToTikTime(60)); }
public void FromTestMethod_Second() { Assert.AreEqual("10s", TikTimeHelper.ToTikTime(10)); }
/// <summary> /// ToString override to make life more easy. /// </summary> public override string ToString() { return(string.Format("{0} ....... {1}", Host, TikTimeHelper.FromTikTimeToSeconds(Time))); }
public void FromTestMethod_None() { Assert.AreEqual("none", TikTimeHelper.ToTikTime(0)); }
public void ToTestMethod_Week() { Assert.AreEqual((3600 * 24 * 7), TikTimeHelper.FromTikTime("1w")); }
public void FromTestMethod_Week() { Assert.AreEqual("1w", TikTimeHelper.ToTikTime((3600 * 24 * 7))); }
public void ToTestMethod_Over1Year() { Assert.AreEqual((3600 * 24 * 500), TikTimeHelper.FromTikTime("71w3d")); }
public void FromTestMethod_AllFields() { Assert.AreEqual("1w3d1h2m1s", TikTimeHelper.ToTikTime((1 + 120 + 3600 + 3600 * 24 * 10))); }