DbConvert.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System.Data.Common;
  2. using System.Data.SqlClient;
  3. using System.Linq.Expressions;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. using System.Collections;
  11. using System.Diagnostics.CodeAnalysis;
  12. namespace System.Data.Linq {
  13. public static class DBConvert {
  14. private static Type[] StringArg = new Type[] { typeof(string) };
  15. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "[....]: Generic parameters are required for strong-typing of the return type.")]
  16. public static T ChangeType<T>(object value) {
  17. return (T)ChangeType(value, typeof(T));
  18. }
  19. [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "[....]: Cast is dependent on node type and casts do not happen unecessarily in a single code path.")]
  20. [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "These issues are related to our use of if-then and case statements for node types, which adds to the complexity count however when reviewed they are easy to navigate and understand.")]
  21. public static object ChangeType(object value, Type type) {
  22. if (value == null)
  23. return null;
  24. MethodInfo mi;
  25. Type toType = System.Data.Linq.SqlClient.TypeSystem.GetNonNullableType(type);
  26. Type fromType = value.GetType();
  27. if (toType.IsAssignableFrom(fromType))
  28. return value;
  29. if (toType == typeof(Binary)) {
  30. if (fromType == typeof(byte[])) {
  31. return new Binary((byte[])value);
  32. }
  33. else if (fromType == typeof(Guid)) {
  34. return new Binary(((Guid)value).ToByteArray());
  35. }
  36. else {
  37. BinaryFormatter formatter = new BinaryFormatter();
  38. byte[] streamArray;
  39. using (MemoryStream stream = new MemoryStream()) {
  40. formatter.Serialize(stream, value);
  41. streamArray = stream.ToArray();
  42. }
  43. return new Binary(streamArray);
  44. }
  45. }
  46. else if (toType == typeof(byte[])) {
  47. if (fromType == typeof(Binary)) {
  48. return ((Binary)value).ToArray();
  49. }
  50. else if (fromType == typeof(Guid)) {
  51. return ((Guid)value).ToByteArray();
  52. }
  53. else {
  54. BinaryFormatter formatter = new BinaryFormatter();
  55. byte[] returnValue;
  56. using (MemoryStream stream = new MemoryStream()) {
  57. formatter.Serialize(stream, value);
  58. returnValue = stream.ToArray();
  59. }
  60. return returnValue;
  61. }
  62. }
  63. else if (fromType == typeof(byte[])) {
  64. if (toType == typeof(Guid)) {
  65. return new Guid((byte[])value);
  66. }
  67. else {
  68. BinaryFormatter formatter = new BinaryFormatter();
  69. object returnValue;
  70. using (MemoryStream stream = new MemoryStream((byte[])value)) {
  71. returnValue = ChangeType(formatter.Deserialize(stream), toType);
  72. }
  73. return returnValue;
  74. }
  75. }
  76. else if (fromType == typeof(Binary)) {
  77. if (toType == typeof(Guid)) {
  78. return new Guid(((Binary)value).ToArray());
  79. }
  80. else {
  81. BinaryFormatter formatter = new BinaryFormatter();
  82. using (MemoryStream stream = new MemoryStream(((Binary)value).ToArray(), false)) {
  83. return ChangeType(formatter.Deserialize(stream), toType);
  84. }
  85. }
  86. }
  87. else if (toType.IsEnum) {
  88. if (fromType == typeof(string)) {
  89. string text = ((string)value).Trim();
  90. return Enum.Parse(toType, text);
  91. }
  92. else {
  93. return Enum.ToObject(toType, Convert.ChangeType(value, Enum.GetUnderlyingType(toType), Globalization.CultureInfo.InvariantCulture));
  94. }
  95. }
  96. else if (fromType.IsEnum) {
  97. if (toType == typeof(string)) {
  98. return Enum.GetName(fromType, value);
  99. }
  100. else {
  101. return Convert.ChangeType(Convert.ChangeType(value,
  102. Enum.GetUnderlyingType(fromType),
  103. Globalization.CultureInfo.InvariantCulture),
  104. toType,
  105. Globalization.CultureInfo.InvariantCulture);
  106. }
  107. }
  108. else if (toType == typeof(TimeSpan)) {
  109. if (fromType == typeof(string)) {
  110. return TimeSpan.Parse(value.ToString(), Globalization.CultureInfo.InvariantCulture);
  111. }
  112. else if (fromType == typeof(DateTime)) {
  113. return DateTime.Parse(value.ToString(), Globalization.CultureInfo.InvariantCulture).TimeOfDay;
  114. }
  115. else if (fromType == typeof(DateTimeOffset)) {
  116. return DateTimeOffset.Parse(value.ToString(), Globalization.CultureInfo.InvariantCulture).TimeOfDay;
  117. }
  118. else {
  119. return new TimeSpan((long)Convert.ChangeType(value, typeof(long), Globalization.CultureInfo.InvariantCulture));
  120. }
  121. }
  122. else if (fromType == typeof(TimeSpan)) {
  123. if (toType == typeof(string)) {
  124. return ((TimeSpan)value).ToString("", Globalization.CultureInfo.InvariantCulture);
  125. }
  126. else if (toType == typeof(DateTime)) {
  127. DateTime dt = new DateTime();
  128. return dt.Add((TimeSpan)value);
  129. }
  130. else if (toType == typeof(DateTimeOffset)) {
  131. DateTimeOffset dto = new DateTimeOffset();
  132. return dto.Add((TimeSpan)value);
  133. }
  134. else {
  135. return Convert.ChangeType(((TimeSpan)value).Ticks, toType, Globalization.CultureInfo.InvariantCulture);
  136. }
  137. }
  138. else if (toType == typeof(DateTime) && fromType == typeof(DateTimeOffset)) {
  139. return ((DateTimeOffset)value).DateTime;
  140. }
  141. else if (toType == typeof(DateTimeOffset) && fromType == typeof(DateTime)) {
  142. return new DateTimeOffset((DateTime)value);
  143. }
  144. else if (toType == typeof(string) && !(typeof(IConvertible).IsAssignableFrom(fromType))) {
  145. if (fromType == typeof(char[])) {
  146. return new String((char[])value);
  147. }
  148. else {
  149. return value.ToString();
  150. }
  151. }
  152. else if (fromType == typeof(string)) {
  153. if (toType == typeof(Guid)) {
  154. return new Guid((string)value);
  155. }
  156. else if (toType == typeof(char[])) {
  157. return ((String)value).ToCharArray();
  158. }
  159. else if (toType == typeof(System.Xml.Linq.XDocument) && (string)value == string.Empty) {
  160. return new System.Xml.Linq.XDocument();
  161. }
  162. else if (!(typeof(IConvertible).IsAssignableFrom(toType)) &&
  163. (mi = toType.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, StringArg, null)) != null) {
  164. try {
  165. return SecurityUtils.MethodInfoInvoke(mi, null, new object[] { value });
  166. }
  167. catch (TargetInvocationException t) {
  168. throw t.GetBaseException();
  169. }
  170. }
  171. else {
  172. return Convert.ChangeType(value, toType, Globalization.CultureInfo.InvariantCulture);
  173. }
  174. }
  175. else if (toType.IsGenericType && toType.GetGenericTypeDefinition() == typeof(IQueryable<>)
  176. && typeof(IEnumerable<>).MakeGenericType(toType.GetGenericArguments()[0]).IsAssignableFrom(fromType)
  177. ) {
  178. return Queryable.AsQueryable((IEnumerable)value);
  179. }
  180. else {
  181. try {
  182. return Convert.ChangeType(value, toType, Globalization.CultureInfo.InvariantCulture);
  183. } catch (InvalidCastException) {
  184. throw Error.CouldNotConvert(fromType, toType);
  185. }
  186. }
  187. }
  188. }
  189. }