Conversion.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace System.Linq.jvm
  6. {
  7. internal class Conversion
  8. {
  9. internal static object ConvertPrimitiveUnChecked (Type from, Type to, object value)
  10. {
  11. unchecked {
  12. switch (Type.GetTypeCode (from)) {
  13. case TypeCode.Byte:
  14. return ConvertByte ((byte) value, to);
  15. case TypeCode.Char:
  16. return ConvertChar ((char) value, to);
  17. case TypeCode.Decimal:
  18. return ConvertDecimal ((decimal) value, to);
  19. case TypeCode.Double:
  20. return ConvertDouble ((double) value, to);
  21. case TypeCode.Int16:
  22. return ConvertShort ((short) value, to);
  23. case TypeCode.Int32:
  24. return ConvertInt ((int) value, to);
  25. case TypeCode.Int64:
  26. return ConvertLong ((long) value, to);
  27. case TypeCode.SByte:
  28. return ConvertSByte ((sbyte) value, to);
  29. case TypeCode.Single:
  30. return ConvertFloat ((float) value, to);
  31. case TypeCode.UInt16:
  32. return ConvertUShort ((ushort) value, to);
  33. case TypeCode.UInt32:
  34. return ConvertUInt ((uint) value, to);
  35. case TypeCode.UInt64:
  36. return ConvertULong ((ulong) value, to);
  37. default:
  38. throw new NotImplementedException ();
  39. }
  40. }
  41. }
  42. static object ConvertByte (byte b, Type to)
  43. {
  44. unchecked {
  45. switch (Type.GetTypeCode (to)) {
  46. case TypeCode.Byte:
  47. return (byte) b;
  48. case TypeCode.Char:
  49. return (char) b;
  50. case TypeCode.Decimal:
  51. return (decimal) b;
  52. case TypeCode.Double:
  53. return (double) b;
  54. case TypeCode.Int16:
  55. return (short) b;
  56. case TypeCode.Int32:
  57. return (int) b;
  58. case TypeCode.Int64:
  59. return (long) b;
  60. case TypeCode.SByte:
  61. return (sbyte) b;
  62. case TypeCode.Single:
  63. return (float) b;
  64. case TypeCode.UInt16:
  65. return (ushort) b;
  66. case TypeCode.UInt32:
  67. return (uint) b;
  68. case TypeCode.UInt64:
  69. return (ulong) b;
  70. }
  71. return null;
  72. }
  73. }
  74. static object ConvertChar (char b, Type to)
  75. {
  76. unchecked {
  77. switch (Type.GetTypeCode (to)) {
  78. case TypeCode.Byte:
  79. return (byte) b;
  80. case TypeCode.Char:
  81. return (char) b;
  82. case TypeCode.Decimal:
  83. return (decimal) b;
  84. case TypeCode.Double:
  85. return (double) b;
  86. case TypeCode.Int16:
  87. return (short) b;
  88. case TypeCode.Int32:
  89. return (int) b;
  90. case TypeCode.Int64:
  91. return (long) b;
  92. case TypeCode.SByte:
  93. return (sbyte) b;
  94. case TypeCode.Single:
  95. return (float) b;
  96. case TypeCode.UInt16:
  97. return (ushort) b;
  98. case TypeCode.UInt32:
  99. return (uint) b;
  100. case TypeCode.UInt64:
  101. return (ulong) b;
  102. }
  103. return null;
  104. }
  105. }
  106. static object ConvertDecimal (decimal b, Type to)
  107. {
  108. unchecked {
  109. switch (Type.GetTypeCode (to)) {
  110. case TypeCode.Byte:
  111. return (byte) b;
  112. case TypeCode.Char:
  113. return (char) b;
  114. case TypeCode.Decimal:
  115. return (decimal) b;
  116. case TypeCode.Double:
  117. return (double) b;
  118. case TypeCode.Int16:
  119. return (short) b;
  120. case TypeCode.Int32:
  121. return (int) b;
  122. case TypeCode.Int64:
  123. return (long) b;
  124. case TypeCode.SByte:
  125. return (sbyte) b;
  126. case TypeCode.Single:
  127. return (float) b;
  128. case TypeCode.UInt16:
  129. return (ushort) b;
  130. case TypeCode.UInt32:
  131. return (uint) b;
  132. case TypeCode.UInt64:
  133. return (ulong) b;
  134. }
  135. return null;
  136. }
  137. }
  138. static object ConvertDouble (double b, Type to)
  139. {
  140. unchecked {
  141. switch (Type.GetTypeCode (to)) {
  142. case TypeCode.Byte:
  143. return (byte) b;
  144. case TypeCode.Char:
  145. return (char) b;
  146. case TypeCode.Decimal:
  147. return (decimal) b;
  148. case TypeCode.Double:
  149. return (double) b;
  150. case TypeCode.Int16:
  151. return (short) b;
  152. case TypeCode.Int32:
  153. return (int) b;
  154. case TypeCode.Int64:
  155. return (long) b;
  156. case TypeCode.SByte:
  157. return (sbyte) b;
  158. case TypeCode.Single:
  159. return (float) b;
  160. case TypeCode.UInt16:
  161. return (ushort) b;
  162. case TypeCode.UInt32:
  163. return (uint) b;
  164. case TypeCode.UInt64:
  165. return (ulong) b;
  166. }
  167. return null;
  168. }
  169. }
  170. static object ConvertShort (short b, Type to)
  171. {
  172. unchecked {
  173. switch (Type.GetTypeCode (to)) {
  174. case TypeCode.Byte:
  175. return (byte) b;
  176. case TypeCode.Char:
  177. return (char) b;
  178. case TypeCode.Decimal:
  179. return (decimal) b;
  180. case TypeCode.Double:
  181. return (double) b;
  182. case TypeCode.Int16:
  183. return (short) b;
  184. case TypeCode.Int32:
  185. return (int) b;
  186. case TypeCode.Int64:
  187. return (long) b;
  188. case TypeCode.SByte:
  189. return (sbyte) b;
  190. case TypeCode.Single:
  191. return (float) b;
  192. case TypeCode.UInt16:
  193. return (ushort) b;
  194. case TypeCode.UInt32:
  195. return (uint) b;
  196. case TypeCode.UInt64:
  197. return (ulong) b;
  198. }
  199. return null;
  200. }
  201. }
  202. static object ConvertInt (int b, Type to)
  203. {
  204. unchecked {
  205. switch (Type.GetTypeCode (to)) {
  206. case TypeCode.Byte:
  207. return (byte) b;
  208. case TypeCode.Char:
  209. return (char) b;
  210. case TypeCode.Decimal:
  211. return (decimal) b;
  212. case TypeCode.Double:
  213. return (double) b;
  214. case TypeCode.Int16:
  215. return (short) b;
  216. case TypeCode.Int32:
  217. return (int) b;
  218. case TypeCode.Int64:
  219. return (long) b;
  220. case TypeCode.SByte:
  221. return (sbyte) b;
  222. case TypeCode.Single:
  223. return (float) b;
  224. case TypeCode.UInt16:
  225. return (ushort) b;
  226. case TypeCode.UInt32:
  227. return (uint) b;
  228. case TypeCode.UInt64:
  229. return (ulong) b;
  230. }
  231. return null;
  232. }
  233. }
  234. static object ConvertLong (long b, Type to)
  235. {
  236. unchecked {
  237. switch (Type.GetTypeCode (to)) {
  238. case TypeCode.Byte:
  239. return (byte) b;
  240. case TypeCode.Char:
  241. return (char) b;
  242. case TypeCode.Decimal:
  243. return (decimal) b;
  244. case TypeCode.Double:
  245. return (double) b;
  246. case TypeCode.Int16:
  247. return (short) b;
  248. case TypeCode.Int32:
  249. return (int) b;
  250. case TypeCode.Int64:
  251. return (long) b;
  252. case TypeCode.SByte:
  253. return (sbyte) b;
  254. case TypeCode.Single:
  255. return (float) b;
  256. case TypeCode.UInt16:
  257. return (ushort) b;
  258. case TypeCode.UInt32:
  259. return (uint) b;
  260. case TypeCode.UInt64:
  261. return (ulong) b;
  262. }
  263. return null;
  264. }
  265. }
  266. static object ConvertSByte (sbyte b, Type to)
  267. {
  268. unchecked {
  269. switch (Type.GetTypeCode (to)) {
  270. case TypeCode.Byte:
  271. return (byte) b;
  272. case TypeCode.Char:
  273. return (char) b;
  274. case TypeCode.Decimal:
  275. return (decimal) b;
  276. case TypeCode.Double:
  277. return (double) b;
  278. case TypeCode.Int16:
  279. return (short) b;
  280. case TypeCode.Int32:
  281. return (int) b;
  282. case TypeCode.Int64:
  283. return (long) b;
  284. case TypeCode.SByte:
  285. return (sbyte) b;
  286. case TypeCode.Single:
  287. return (float) b;
  288. case TypeCode.UInt16:
  289. return (ushort) b;
  290. case TypeCode.UInt32:
  291. return (uint) b;
  292. case TypeCode.UInt64:
  293. return (ulong) b;
  294. }
  295. return null;
  296. }
  297. }
  298. static object ConvertFloat (float b, Type to)
  299. {
  300. unchecked {
  301. switch (Type.GetTypeCode (to)) {
  302. case TypeCode.Byte:
  303. return (byte) b;
  304. case TypeCode.Char:
  305. return (char) b;
  306. case TypeCode.Decimal:
  307. return (decimal) b;
  308. case TypeCode.Double:
  309. return (double) b;
  310. case TypeCode.Int16:
  311. return (short) b;
  312. case TypeCode.Int32:
  313. return (int) b;
  314. case TypeCode.Int64:
  315. return (long) b;
  316. case TypeCode.SByte:
  317. return (sbyte) b;
  318. case TypeCode.Single:
  319. return (float) b;
  320. case TypeCode.UInt16:
  321. return (ushort) b;
  322. case TypeCode.UInt32:
  323. return (uint) b;
  324. case TypeCode.UInt64:
  325. return (ulong) b;
  326. }
  327. return null;
  328. }
  329. }
  330. static object ConvertUShort (ushort b, Type to)
  331. {
  332. unchecked {
  333. switch (Type.GetTypeCode (to)) {
  334. case TypeCode.Byte:
  335. return (byte) b;
  336. case TypeCode.Char:
  337. return (char) b;
  338. case TypeCode.Decimal:
  339. return (decimal) b;
  340. case TypeCode.Double:
  341. return (double) b;
  342. case TypeCode.Int16:
  343. return (short) b;
  344. case TypeCode.Int32:
  345. return (int) b;
  346. case TypeCode.Int64:
  347. return (long) b;
  348. case TypeCode.SByte:
  349. return (sbyte) b;
  350. case TypeCode.Single:
  351. return (float) b;
  352. case TypeCode.UInt16:
  353. return (ushort) b;
  354. case TypeCode.UInt32:
  355. return (uint) b;
  356. case TypeCode.UInt64:
  357. return (ulong) b;
  358. }
  359. return null;
  360. }
  361. }
  362. static object ConvertUInt (uint b, Type to)
  363. {
  364. unchecked {
  365. switch (Type.GetTypeCode (to)) {
  366. case TypeCode.Byte:
  367. return (byte) b;
  368. case TypeCode.Char:
  369. return (char) b;
  370. case TypeCode.Decimal:
  371. return (decimal) b;
  372. case TypeCode.Double:
  373. return (double) b;
  374. case TypeCode.Int16:
  375. return (short) b;
  376. case TypeCode.Int32:
  377. return (int) b;
  378. case TypeCode.Int64:
  379. return (long) b;
  380. case TypeCode.SByte:
  381. return (sbyte) b;
  382. case TypeCode.Single:
  383. return (float) b;
  384. case TypeCode.UInt16:
  385. return (ushort) b;
  386. case TypeCode.UInt32:
  387. return (uint) b;
  388. case TypeCode.UInt64:
  389. return (ulong) b;
  390. }
  391. return null;
  392. }
  393. }
  394. static object ConvertULong (ulong b, Type to)
  395. {
  396. unchecked {
  397. switch (Type.GetTypeCode (to)) {
  398. case TypeCode.Byte:
  399. return (byte) b;
  400. case TypeCode.Char:
  401. return (char) b;
  402. case TypeCode.Decimal:
  403. return (decimal) b;
  404. case TypeCode.Double:
  405. return (double) b;
  406. case TypeCode.Int16:
  407. return (short) b;
  408. case TypeCode.Int32:
  409. return (int) b;
  410. case TypeCode.Int64:
  411. return (long) b;
  412. case TypeCode.SByte:
  413. return (sbyte) b;
  414. case TypeCode.Single:
  415. return (float) b;
  416. case TypeCode.UInt16:
  417. return (ushort) b;
  418. case TypeCode.UInt32:
  419. return (uint) b;
  420. case TypeCode.UInt64:
  421. return (ulong) b;
  422. }
  423. return null;
  424. }
  425. }
  426. }
  427. }