public static object ChangeType(object value, Type type) { if (value.GetType() == typeof(DBNull) && type == typeof(String)) { return(String.Empty); } else if (value.GetType() == typeof(DBNull)) { return(null); } switch (type.Name) { case "PhoneNumber": return((PhoneNumber)(string)value); case "EmailAddress": return((EmailAddress)(string)value); case "URL": return((URL)(string)value); case "PostalAddress": return(PostalAddress.Deserialize((string)value)); case "Boolean": value = ((string)value).Replace("0", "false"); value = ((string)value).Replace("1", "true"); break; case "SimpleXMLNameValueCollection": return(new SimpleXMLNameValueCollection(SqlConvert.ToXML((string)value))); } try { return(Convert.ChangeType(value, type)); } catch { if (type == typeof(String)) { return(String.Empty); } else { return(null); } } }
/// <summary> /// Returns PostalAddress from object /// </summary> public static PostalAddress ToPostalAddress(object obj) { if (obj == null) { return(new PostalAddress()); } else if (Convert.IsDBNull(obj)) { return(new PostalAddress()); } else if (obj.GetType() == typeof(PostalAddress)) { return((PostalAddress)obj); } else { return(PostalAddress.Deserialize((string)obj)); } }