Conversion.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. //
  2. // Conversion.cs
  3. //
  4. // (C) 2008 Mainsoft, Inc. (http://www.mainsoft.com)
  5. // (C) 2008 db4objects, Inc. (http://www.db4o.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. namespace System.Linq.jvm {
  28. class Conversion {
  29. public static object ConvertPrimitiveUnChecked (Type from, Type to, object value)
  30. {
  31. unchecked {
  32. switch (Type.GetTypeCode (from)) {
  33. case TypeCode.Byte:
  34. return ConvertByte ((byte) value, to);
  35. case TypeCode.Char:
  36. return ConvertChar ((char) value, to);
  37. case TypeCode.Decimal:
  38. return ConvertDecimal ((decimal) value, to);
  39. case TypeCode.Double:
  40. return ConvertDouble ((double) value, to);
  41. case TypeCode.Int16:
  42. return ConvertShort ((short) value, to);
  43. case TypeCode.Int32:
  44. return ConvertInt ((int) value, to);
  45. case TypeCode.Int64:
  46. return ConvertLong ((long) value, to);
  47. case TypeCode.SByte:
  48. return ConvertSByte ((sbyte) value, to);
  49. case TypeCode.Single:
  50. return ConvertFloat ((float) value, to);
  51. case TypeCode.UInt16:
  52. return ConvertUShort ((ushort) value, to);
  53. case TypeCode.UInt32:
  54. return ConvertUInt ((uint) value, to);
  55. case TypeCode.UInt64:
  56. return ConvertULong ((ulong) value, to);
  57. default:
  58. throw new NotImplementedException ();
  59. }
  60. }
  61. }
  62. static object ConvertByte (byte b, Type to)
  63. {
  64. unchecked {
  65. switch (Type.GetTypeCode (to)) {
  66. case TypeCode.Byte:
  67. return (byte) b;
  68. case TypeCode.Char:
  69. return (char) b;
  70. case TypeCode.Decimal:
  71. return (decimal) b;
  72. case TypeCode.Double:
  73. return (double) b;
  74. case TypeCode.Int16:
  75. return (short) b;
  76. case TypeCode.Int32:
  77. return (int) b;
  78. case TypeCode.Int64:
  79. return (long) b;
  80. case TypeCode.SByte:
  81. return (sbyte) b;
  82. case TypeCode.Single:
  83. return (float) b;
  84. case TypeCode.UInt16:
  85. return (ushort) b;
  86. case TypeCode.UInt32:
  87. return (uint) b;
  88. case TypeCode.UInt64:
  89. return (ulong) b;
  90. }
  91. return null;
  92. }
  93. }
  94. static object ConvertChar (char b, Type to)
  95. {
  96. unchecked {
  97. switch (Type.GetTypeCode (to)) {
  98. case TypeCode.Byte:
  99. return (byte) b;
  100. case TypeCode.Char:
  101. return (char) b;
  102. case TypeCode.Decimal:
  103. return (decimal) b;
  104. case TypeCode.Double:
  105. return (double) b;
  106. case TypeCode.Int16:
  107. return (short) b;
  108. case TypeCode.Int32:
  109. return (int) b;
  110. case TypeCode.Int64:
  111. return (long) b;
  112. case TypeCode.SByte:
  113. return (sbyte) b;
  114. case TypeCode.Single:
  115. return (float) b;
  116. case TypeCode.UInt16:
  117. return (ushort) b;
  118. case TypeCode.UInt32:
  119. return (uint) b;
  120. case TypeCode.UInt64:
  121. return (ulong) b;
  122. }
  123. return null;
  124. }
  125. }
  126. static object ConvertDecimal (decimal b, Type to)
  127. {
  128. unchecked {
  129. switch (Type.GetTypeCode (to)) {
  130. case TypeCode.Byte:
  131. return (byte) b;
  132. case TypeCode.Char:
  133. return (char) (short) b;
  134. case TypeCode.Decimal:
  135. return (decimal) b;
  136. case TypeCode.Double:
  137. return (double) b;
  138. case TypeCode.Int16:
  139. return (short) b;
  140. case TypeCode.Int32:
  141. return (int) b;
  142. case TypeCode.Int64:
  143. return (long) b;
  144. case TypeCode.SByte:
  145. return (sbyte) b;
  146. case TypeCode.Single:
  147. return (float) b;
  148. case TypeCode.UInt16:
  149. return (ushort) b;
  150. case TypeCode.UInt32:
  151. return (uint) b;
  152. case TypeCode.UInt64:
  153. return (ulong) b;
  154. }
  155. return null;
  156. }
  157. }
  158. static object ConvertDouble (double b, Type to)
  159. {
  160. unchecked {
  161. switch (Type.GetTypeCode (to)) {
  162. case TypeCode.Byte:
  163. return (byte) b;
  164. case TypeCode.Char:
  165. return (char) b;
  166. case TypeCode.Decimal:
  167. return (decimal) b;
  168. case TypeCode.Double:
  169. return (double) b;
  170. case TypeCode.Int16:
  171. return (short) b;
  172. case TypeCode.Int32:
  173. return (int) b;
  174. case TypeCode.Int64:
  175. return (long) b;
  176. case TypeCode.SByte:
  177. return (sbyte) b;
  178. case TypeCode.Single:
  179. return (float) b;
  180. case TypeCode.UInt16:
  181. return (ushort) b;
  182. case TypeCode.UInt32:
  183. return (uint) b;
  184. case TypeCode.UInt64:
  185. return (ulong) b;
  186. }
  187. return null;
  188. }
  189. }
  190. static object ConvertShort (short b, Type to)
  191. {
  192. unchecked {
  193. switch (Type.GetTypeCode (to)) {
  194. case TypeCode.Byte:
  195. return (byte) b;
  196. case TypeCode.Char:
  197. return (char) b;
  198. case TypeCode.Decimal:
  199. return (decimal) b;
  200. case TypeCode.Double:
  201. return (double) b;
  202. case TypeCode.Int16:
  203. return (short) b;
  204. case TypeCode.Int32:
  205. return (int) b;
  206. case TypeCode.Int64:
  207. return (long) b;
  208. case TypeCode.SByte:
  209. return (sbyte) b;
  210. case TypeCode.Single:
  211. return (float) b;
  212. case TypeCode.UInt16:
  213. return (ushort) b;
  214. case TypeCode.UInt32:
  215. return (uint) b;
  216. case TypeCode.UInt64:
  217. return (ulong) b;
  218. }
  219. return null;
  220. }
  221. }
  222. static object ConvertInt (int b, Type to)
  223. {
  224. unchecked {
  225. switch (Type.GetTypeCode (to)) {
  226. case TypeCode.Byte:
  227. return (byte) b;
  228. case TypeCode.Char:
  229. return (char) b;
  230. case TypeCode.Decimal:
  231. return (decimal) b;
  232. case TypeCode.Double:
  233. return (double) b;
  234. case TypeCode.Int16:
  235. return (short) b;
  236. case TypeCode.Int32:
  237. return (int) b;
  238. case TypeCode.Int64:
  239. return (long) b;
  240. case TypeCode.SByte:
  241. return (sbyte) b;
  242. case TypeCode.Single:
  243. return (float) b;
  244. case TypeCode.UInt16:
  245. return (ushort) b;
  246. case TypeCode.UInt32:
  247. return (uint) b;
  248. case TypeCode.UInt64:
  249. return (ulong) b;
  250. }
  251. return null;
  252. }
  253. }
  254. static object ConvertLong (long b, Type to)
  255. {
  256. unchecked {
  257. switch (Type.GetTypeCode (to)) {
  258. case TypeCode.Byte:
  259. return (byte) b;
  260. case TypeCode.Char:
  261. return (char) b;
  262. case TypeCode.Decimal:
  263. return (decimal) b;
  264. case TypeCode.Double:
  265. return (double) b;
  266. case TypeCode.Int16:
  267. return (short) b;
  268. case TypeCode.Int32:
  269. return (int) b;
  270. case TypeCode.Int64:
  271. return (long) b;
  272. case TypeCode.SByte:
  273. return (sbyte) b;
  274. case TypeCode.Single:
  275. return (float) b;
  276. case TypeCode.UInt16:
  277. return (ushort) b;
  278. case TypeCode.UInt32:
  279. return (uint) b;
  280. case TypeCode.UInt64:
  281. return (ulong) b;
  282. }
  283. return null;
  284. }
  285. }
  286. static object ConvertSByte (sbyte b, Type to)
  287. {
  288. unchecked {
  289. switch (Type.GetTypeCode (to)) {
  290. case TypeCode.Byte:
  291. return (byte) b;
  292. case TypeCode.Char:
  293. return (char) b;
  294. case TypeCode.Decimal:
  295. return (decimal) b;
  296. case TypeCode.Double:
  297. return (double) b;
  298. case TypeCode.Int16:
  299. return (short) b;
  300. case TypeCode.Int32:
  301. return (int) b;
  302. case TypeCode.Int64:
  303. return (long) b;
  304. case TypeCode.SByte:
  305. return (sbyte) b;
  306. case TypeCode.Single:
  307. return (float) b;
  308. case TypeCode.UInt16:
  309. return (ushort) b;
  310. case TypeCode.UInt32:
  311. return (uint) b;
  312. case TypeCode.UInt64:
  313. return (ulong) b;
  314. }
  315. return null;
  316. }
  317. }
  318. static object ConvertFloat (float b, Type to)
  319. {
  320. unchecked {
  321. switch (Type.GetTypeCode (to)) {
  322. case TypeCode.Byte:
  323. return (byte) b;
  324. case TypeCode.Char:
  325. return (char) b;
  326. case TypeCode.Decimal:
  327. return (decimal) b;
  328. case TypeCode.Double:
  329. return (double) b;
  330. case TypeCode.Int16:
  331. return (short) b;
  332. case TypeCode.Int32:
  333. return (int) b;
  334. case TypeCode.Int64:
  335. return (long) b;
  336. case TypeCode.SByte:
  337. return (sbyte) b;
  338. case TypeCode.Single:
  339. return (float) b;
  340. case TypeCode.UInt16:
  341. return (ushort) b;
  342. case TypeCode.UInt32:
  343. return (uint) b;
  344. case TypeCode.UInt64:
  345. return (ulong) b;
  346. }
  347. return null;
  348. }
  349. }
  350. static object ConvertUShort (ushort b, Type to)
  351. {
  352. unchecked {
  353. switch (Type.GetTypeCode (to)) {
  354. case TypeCode.Byte:
  355. return (byte) b;
  356. case TypeCode.Char:
  357. return (char) b;
  358. case TypeCode.Decimal:
  359. return (decimal) b;
  360. case TypeCode.Double:
  361. return (double) b;
  362. case TypeCode.Int16:
  363. return (short) b;
  364. case TypeCode.Int32:
  365. return (int) b;
  366. case TypeCode.Int64:
  367. return (long) b;
  368. case TypeCode.SByte:
  369. return (sbyte) b;
  370. case TypeCode.Single:
  371. return (float) b;
  372. case TypeCode.UInt16:
  373. return (ushort) b;
  374. case TypeCode.UInt32:
  375. return (uint) b;
  376. case TypeCode.UInt64:
  377. return (ulong) b;
  378. }
  379. return null;
  380. }
  381. }
  382. static object ConvertUInt (uint b, Type to)
  383. {
  384. unchecked {
  385. switch (Type.GetTypeCode (to)) {
  386. case TypeCode.Byte:
  387. return (byte) b;
  388. case TypeCode.Char:
  389. return (char) b;
  390. case TypeCode.Decimal:
  391. return (decimal) b;
  392. case TypeCode.Double:
  393. return (double) b;
  394. case TypeCode.Int16:
  395. return (short) b;
  396. case TypeCode.Int32:
  397. return (int) b;
  398. case TypeCode.Int64:
  399. return (long) b;
  400. case TypeCode.SByte:
  401. return (sbyte) b;
  402. case TypeCode.Single:
  403. return (float) b;
  404. case TypeCode.UInt16:
  405. return (ushort) b;
  406. case TypeCode.UInt32:
  407. return (uint) b;
  408. case TypeCode.UInt64:
  409. return (ulong) b;
  410. }
  411. return null;
  412. }
  413. }
  414. static object ConvertULong (ulong b, Type to)
  415. {
  416. unchecked {
  417. switch (Type.GetTypeCode (to)) {
  418. case TypeCode.Byte:
  419. return (byte) b;
  420. case TypeCode.Char:
  421. return (char) b;
  422. case TypeCode.Decimal:
  423. return (decimal) b;
  424. case TypeCode.Double:
  425. return (double) b;
  426. case TypeCode.Int16:
  427. return (short) b;
  428. case TypeCode.Int32:
  429. return (int) b;
  430. case TypeCode.Int64:
  431. return (long) b;
  432. case TypeCode.SByte:
  433. return (sbyte) b;
  434. case TypeCode.Single:
  435. return (float) b;
  436. case TypeCode.UInt16:
  437. return (ushort) b;
  438. case TypeCode.UInt32:
  439. return (uint) b;
  440. case TypeCode.UInt64:
  441. return (ulong) b;
  442. }
  443. return null;
  444. }
  445. }
  446. }
  447. }