Serializer.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Drawing;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. using System.Fire.Serialize.Property;
  9. namespace System.Fire.CSV {
  10. public class Serializer {
  11. protected const StringEncoding DefaultStringEncoding = StringEncoding.UTF8;
  12. /// <summary>
  13. /// Serialize an object
  14. /// </summary>
  15. /// <param name="FilePath">The Filepath to write the data to</param>
  16. /// <param name="o">The Object to serialize</param>
  17. protected static void SerializeObject(string FilePath, object o) {
  18. Stream str = new FileStream(FilePath, FileMode.Create);
  19. SerializeObject(str, o, DefaultStringEncoding);
  20. str.Close();
  21. }
  22. /// <summary>
  23. /// Serialize an object
  24. /// </summary>
  25. /// <param name="str">Stream Object to write the Data in</param>
  26. /// <param name="o">The Object to serialize</param>
  27. protected static void SerializeObject(Stream str, object o) {
  28. SerializeObject(str, o, DefaultStringEncoding);
  29. }
  30. /// <summary>
  31. /// Serialize an object
  32. /// </summary>
  33. /// <param name="FilePath">The Filepath to write the data to</param>
  34. /// <param name="o">The Object to serialize</param>
  35. protected static void SerializeObject(string FilePath, object o, StringEncoding encode) {
  36. Stream str = new FileStream(FilePath, FileMode.Create);
  37. SerializeObject(str, o, encode);
  38. str.Close();
  39. }
  40. /// <summary>
  41. /// Serialize an object
  42. /// </summary>
  43. /// <param name="str">Stream Object to write the Data in</param>
  44. /// <param name="o">The Object to serialize</param>
  45. /// <param name="size">The size of arrays</param>
  46. protected static void SerializeObject(Stream str, object o, StringEncoding encode) {
  47. if (o == null) {
  48. return;
  49. }
  50. TypeInfo typeinfo = o.GetType().GetTypeInfo();
  51. var SerializeMethod1 =
  52. typeinfo.DeclaredMethods.FirstOrDefault(x =>
  53. x.Name == "ToCSVStream" &&
  54. x.GetParameters().Length == 1 &&
  55. x.GetParameters()[0].ParameterType == typeof(Stream) &&
  56. x.IsStatic == false);
  57. var SerializeMethod3 =
  58. typeinfo.DeclaredMethods.FirstOrDefault(x =>
  59. x.Name == "ToCSVStream" &&
  60. x.GetParameters().Length == 2 &&
  61. x.GetParameters()[0].ParameterType == typeof(Stream) &&
  62. x.GetParameters()[1].ParameterType == typeof(StringEncoding) &&
  63. x.IsStatic == false);
  64. /*if (typeinfo.IsGenericType) {
  65. System.Diagnostics.Debug.WriteLine("- Generic Type");
  66. Type GenType = typeinfo.GetGenericTypeDefinition();
  67. Type[] interfaces = GenType.GetInterfaces();
  68. for (int i = 0; i < interfaces.Length; i++)
  69. System.Diagnostics.Debug.WriteLine("- - " + interfaces[i].Name);
  70. }*/
  71. if (SerializeMethod3 != null) {
  72. SerializeMethod3.Invoke(o, new object[] { str, encode });
  73. } else if (SerializeMethod1 != null) {
  74. SerializeMethod1.Invoke(o, new object[] { str });
  75. } else {
  76. if (o.GetType() == typeof(string)) {
  77. string s = (string)o;
  78. s = "\"" + s.Replace("\"","\\\"") + "\"";
  79. byte[] b;
  80. if (encode == StringEncoding.UTF8)
  81. b = Text.Encoding.UTF8.GetBytes(s);
  82. else if (encode == StringEncoding.UTF7)
  83. b = Text.Encoding.UTF7.GetBytes(s);
  84. else if (encode == StringEncoding.UTF32)
  85. b = Text.Encoding.UTF32.GetBytes(s);
  86. else if (encode == StringEncoding.Asci)
  87. b = Text.Encoding.ASCII.GetBytes(s);
  88. else
  89. b = Text.Encoding.Unicode.GetBytes(s); //Enumerable.ToArray((dynamic)o);
  90. str.Write(b,0,b.Length);
  91. } else if (o.GetType() == typeof(MemoryStream)) {
  92. MemoryStream mem = (MemoryStream)o;
  93. if (mem.Length > uint.MaxValue) {
  94. SerializeObject(str, (uint)0, encode);
  95. } else {
  96. SerializeObject(str, (uint)mem.Length, encode);
  97. long xpos = mem.Position;
  98. mem.Position = 0;
  99. while (mem.Position < mem.Length) {
  100. byte[] b = new byte[10000];
  101. int readedBytes = mem.Read(b, 0, 10000);
  102. str.Write(b, 0, readedBytes);
  103. }
  104. mem.Position = xpos; // Reset position to the old value
  105. }
  106. } else if (o.GetType() == typeof(System.Text.RegularExpressions.Regex)) {
  107. System.Diagnostics.Debug.WriteLine("RegEx: " + ((Text.RegularExpressions.Regex)o).ToString());
  108. str.Write(new byte[] { 123 },0,1);
  109. SerializeObject(str, (ushort)((Text.RegularExpressions.Regex)o).Options, encode);
  110. str.Write(new byte[] { 44 }, 0, 1);
  111. SerializeObject(str, ((Text.RegularExpressions.Regex)o).ToString(), encode);
  112. str.Write(new byte[] { 125 },0,1);
  113. } else if (o.GetType() == typeof(Version)) {
  114. System.Diagnostics.Debug.WriteLine("Version");
  115. str.Write(new byte[] { 123 }, 0, 1);
  116. SerializeObject(str, ((Version)o).Major, encode);
  117. str.Write(new byte[] { 44 }, 0, 1);
  118. SerializeObject(str, ((Version)o).Minor, encode);
  119. str.Write(new byte[] { 44 }, 0, 1);
  120. SerializeObject(str, ((Version)o).Build, encode);
  121. str.Write(new byte[] { 44 }, 0, 1);
  122. SerializeObject(str, ((Version)o).Revision, encode);
  123. str.Write(new byte[] { 125 }, 0, 1);
  124. } else if (o.GetType() == typeof(Uri)) {
  125. System.Diagnostics.Debug.WriteLine("Uri");
  126. SerializeObject(str, ((Uri)o).OriginalString, encode);
  127. } else if (o.GetType() == typeof(Rectangle)) {
  128. System.Diagnostics.Debug.WriteLine("Rectangle");
  129. str.Write(new byte[] { 123 }, 0, 1);
  130. SerializeObject(str, ((Rectangle)o).X, encode);
  131. str.Write(new byte[] { 44 }, 0, 1);
  132. SerializeObject(str, ((Rectangle)o).Y, encode);
  133. str.Write(new byte[] { 44 }, 0, 1);
  134. SerializeObject(str, ((Rectangle)o).Width, encode);
  135. str.Write(new byte[] { 44 }, 0, 1);
  136. SerializeObject(str, ((Rectangle)o).Height, encode);
  137. str.Write(new byte[] { 125 }, 0, 1);
  138. } else if (o.GetType() == typeof(RectangleF)) {
  139. System.Diagnostics.Debug.WriteLine("RectangleF");
  140. str.Write(new byte[] { 123 }, 0, 1);
  141. SerializeObject(str, ((RectangleF)o).X, encode);
  142. str.Write(new byte[] { 44 }, 0, 1);
  143. SerializeObject(str, ((RectangleF)o).Y, encode);
  144. str.Write(new byte[] { 44 }, 0, 1);
  145. SerializeObject(str, ((RectangleF)o).Width, encode);
  146. str.Write(new byte[] { 44 }, 0, 1);
  147. SerializeObject(str, ((RectangleF)o).Height, encode);
  148. str.Write(new byte[] { 125 }, 0, 1);
  149. } else if (o.GetType() == typeof(Size)) {
  150. System.Diagnostics.Debug.WriteLine("Size");
  151. str.Write(new byte[] { 123 }, 0, 1);
  152. SerializeObject(str, ((Size)o).Width, encode);
  153. str.Write(new byte[] { 44 }, 0, 1);
  154. SerializeObject(str, ((Size)o).Height, encode);
  155. str.Write(new byte[] { 125 }, 0, 1);
  156. } else if (o.GetType() == typeof(SizeF)) {
  157. System.Diagnostics.Debug.WriteLine("SizeF");
  158. str.Write(new byte[] { 123 }, 0, 1);
  159. SerializeObject(str, ((SizeF)o).Width, encode);
  160. str.Write(new byte[] { 44 }, 0, 1);
  161. SerializeObject(str, ((SizeF)o).Height, encode);
  162. str.Write(new byte[] { 125 }, 0, 1);
  163. } else if (o.GetType() == typeof(Point)) {
  164. System.Diagnostics.Debug.WriteLine("Point");
  165. str.Write(new byte[] { 123 }, 0, 1);
  166. SerializeObject(str, ((Point)o).X, encode);
  167. str.Write(new byte[] { 44 }, 0, 1);
  168. SerializeObject(str, ((Point)o).Y, encode);
  169. str.Write(new byte[] { 125 }, 0, 1);
  170. } else if (o.GetType() == typeof(PointF)) {
  171. System.Diagnostics.Debug.WriteLine("PointF");
  172. str.Write(new byte[] { 123 }, 0, 1);
  173. SerializeObject(str, ((PointF)o).X, encode);
  174. str.Write(new byte[] { 44 }, 0, 1);
  175. SerializeObject(str, ((PointF)o).Y, encode);
  176. str.Write(new byte[] { 125 }, 0, 1);
  177. } else if (o.GetType() == typeof(Color)) {
  178. System.Diagnostics.Debug.WriteLine("Color");
  179. str.Write(new byte[] { 123 }, 0, 1);
  180. SerializeObject(str, ((Color)o).A, encode);
  181. str.Write(new byte[] { 44 }, 0, 1);
  182. SerializeObject(str, ((Color)o).R, encode);
  183. str.Write(new byte[] { 44 }, 0, 1);
  184. SerializeObject(str, ((Color)o).G, encode);
  185. str.Write(new byte[] { 44 }, 0, 1);
  186. SerializeObject(str, ((Color)o).B, encode);
  187. str.Write(new byte[] { 125 }, 0, 1);
  188. } else if (o.GetType() == typeof(System.Globalization.CultureInfo)) {
  189. System.Diagnostics.Debug.WriteLine("CultureInfo");
  190. SerializeObject(str, ((System.Globalization.CultureInfo)o).LCID, encode);
  191. } else if (o.GetType() == typeof(System.Drawing.Font)) {
  192. System.Diagnostics.Debug.WriteLine("Font");
  193. str.Write(new byte[] { 123 }, 0, 1);
  194. SerializeObject(str, (byte)((Font)o).Style, encode);
  195. str.Write(new byte[] { 44 }, 0, 1);
  196. SerializeObject(str, ((Font)o).Size, encode);
  197. str.Write(new byte[] { 44 }, 0, 1);
  198. SerializeObject(str, ((Font)o).Unit, encode);
  199. str.Write(new byte[] { 44 }, 0, 1);
  200. SerializeObject(str, ((Font)o).FontFamily.Name, encode);
  201. str.Write(new byte[] { 125 }, 0, 1);
  202. } else if (o.GetType() == typeof(bool)) {
  203. System.Diagnostics.Debug.WriteLine("Boolean: " + ((bool)o ? "True" : "False"));
  204. if ((bool)o == true) {
  205. str.WriteByte(1);
  206. } else {
  207. str.WriteByte(0);
  208. }
  209. } else if (o.GetType() == typeof(sbyte)) {
  210. System.Diagnostics.Debug.WriteLine("SByte");
  211. SerializeObject(str, ((sbyte)o).ToString(), encode);
  212. } else if (o.GetType() == typeof(char)) {
  213. System.Diagnostics.Debug.WriteLine("Char");
  214. SerializeObject(str, ((char)o).ToString(), encode);
  215. } else if (o.GetType() == typeof(decimal)) {
  216. System.Diagnostics.Debug.WriteLine("Decimal");
  217. SerializeObject(str, ((decimal)o).ToString(), encode);
  218. } else if (o.GetType() == typeof(DateTime)) {
  219. System.Diagnostics.Debug.WriteLine("DateTime");
  220. SerializeObject(str, ((DateTime)o).Ticks, encode);
  221. } else if (typeinfo.IsArray) {
  222. System.Diagnostics.Debug.WriteLine("Array");
  223. SerializeArray(str, (Array)o, encode);
  224. /*} else if (typeinfo.IsGenericType && typeinfo.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable<>)))
  225. SerializeArray(str, Enumerable.ToArray((dynamic)o), size, encode);
  226. */
  227. } else if (typeinfo.IsGenericType) {
  228. bool bIsListable = false;
  229. string iEnumarableName = typeof(IEnumerable<>).Name;
  230. Type[] interfaces = typeinfo.GetGenericTypeDefinition().GetInterfaces();
  231. foreach (Type i in interfaces) {
  232. if (i.Name == iEnumarableName) {
  233. bIsListable = true;
  234. break;
  235. }
  236. }
  237. if (bIsListable) {
  238. SerializeArray(str, Enumerable.ToArray((dynamic)o), encode);
  239. }
  240. } else {
  241. byte[] buf = null;
  242. if (o.GetType() == typeof(byte))
  243. buf = new byte[] { (byte)o };
  244. else if (typeof(BitConverter).GetTypeInfo()
  245. .DeclaredMethods.Any(x =>
  246. x.Name == "GetBytes" &&
  247. x.IsStatic &&
  248. x.GetParameters()
  249. .SingleOrDefault()
  250. .ParameterType == o.GetType()))
  251. buf = BitConverter.GetBytes((dynamic)o);
  252. else {
  253. /*foreach (dynamic dyn in typeinfo.DeclaredFields
  254. .Where(x => (x.IsPublic || x.IsAssembly) && !x.IsStatic)
  255. .Select(x => x.GetValue(o))) {
  256. FieldInfo fi = (FieldInfo)dyn;
  257. System.Diagnostics.Debug.WriteLine("- " + fi.Name + " [" + (fi.IsPublic ? "Y" : "N") + (fi.IsAssembly ? "Y" : "N") + (fi.IsStatic ? "Y" : "N") + "]");
  258. SerializeObject(str, dyn, size, encode);
  259. }*/
  260. foreach (FieldInfo fi in typeinfo.DeclaredFields) {
  261. if ((fi.IsPublic || fi.IsAssembly) && !fi.IsStatic && fi.GetCustomAttribute(typeof(Annotations.SerializeIgnore)) == null) {
  262. System.Diagnostics.Debug.WriteLine("- " + fi.Name + " [" + (fi.IsPublic ? "Y" : "N") + (fi.IsAssembly ? "Y" : "N") + (fi.IsStatic ? "Y" : "N") + "]");
  263. SerializeObject(str, fi.GetValue(o), encode);
  264. }
  265. }
  266. /*foreach(PropertyInfo pi in typeinfo.DeclaredProperties) {
  267. if(pi.CanRead) {
  268. SerializeObject(str, pi.GetValue(o), size, encode);
  269. }
  270. }*/
  271. return;
  272. }
  273. str.Write(buf, 0, buf.Length);
  274. return;
  275. }
  276. }
  277. }
  278. protected static T DeserializeObject<T>(string FilePath) {
  279. return (T)Serializer.DeserializeObject(FilePath, typeof(T));
  280. }
  281. protected static T DeserializeObject<T>(string FilePath, StringEncoding encode) {
  282. return (T)Serializer.DeserializeObject(FilePath, typeof(T), encode);
  283. }
  284. protected static T DeserializeObject<T>(Stream str) {
  285. return (T)Serializer.DeserializeObject(str, typeof(T));
  286. }
  287. protected static T DeserializeObject<T>(Stream str, StringEncoding encode) {
  288. return (T)Serializer.DeserializeObject(str, typeof(T), encode);
  289. }
  290. /// <summary>
  291. /// Deserialze an object
  292. /// </summary>
  293. /// <param name="FilePath">The File that contains</param>
  294. /// <param name="T">The object type to deserialize</param>
  295. /// <returns>Returns the object</returns>
  296. protected static object DeserializeObject(string FilePath, Type T) {
  297. if (File.Exists(FilePath)) {
  298. Stream str = new FileStream(FilePath, FileMode.Open);
  299. if (str.Length > 0) {
  300. var res = DeserializeObject(str, T, DefaultStringEncoding);
  301. str.Close();
  302. return res;
  303. }
  304. str.Close();
  305. }
  306. return null;
  307. }
  308. /// <summary>
  309. /// Deserialze an object
  310. /// </summary>
  311. /// <param name="str">Stream Object to read the Data from</param>
  312. /// <param name="T">The object type to deserialize</param>
  313. /// <returns>Returns the object</returns>
  314. protected static object DeserializeObject(Stream str, Type T) {
  315. if (str.Length > 0)
  316. return DeserializeObject(str, T, DefaultStringEncoding);
  317. return null;
  318. }
  319. /// <summary>
  320. /// Deserialze an object
  321. /// </summary>
  322. /// <param name="FilePath">The File that contains</param>
  323. /// <param name="T">The object type to deserialize</param>
  324. /// <returns>Returns the object</returns>
  325. protected static object DeserializeObject(string FilePath, Type T, StringEncoding encode) {
  326. if (File.Exists(FilePath)) {
  327. Stream str = new FileStream(FilePath, FileMode.Open);
  328. if (str.Length > 0) {
  329. var res = DeserializeObject(str, T, encode);
  330. str.Close();
  331. return res;
  332. }
  333. str.Close();
  334. }
  335. return null;
  336. }
  337. /// <summary>
  338. /// Deserialze an object
  339. /// </summary>
  340. /// <param name="str">Stream Object to read the Data from</param>
  341. /// <param name="T">The object type to deserialize</param>
  342. /// <param name="size">The Size of arrays</param>
  343. /// <returns>Returns the object</returns>
  344. protected static object DeserializeObject(Stream str, Type T, StringEncoding encode) {
  345. if (str.Length == 0)
  346. return null;
  347. var typeinfo = T.GetTypeInfo();
  348. var DeserializeMethod1 =
  349. typeinfo.DeclaredMethods.FirstOrDefault(x =>
  350. x.Name == "FromDataStream" &&
  351. x.GetParameters().Length == 1 &&
  352. x.GetParameters()[0].ParameterType == typeof(Stream) &&
  353. x.IsStatic == true &&
  354. x.ReturnType == T);
  355. var DeserializeMethod3 =
  356. typeinfo.DeclaredMethods.FirstOrDefault(x =>
  357. x.Name == "FromDataStream" &&
  358. x.GetParameters().Length == 2 &&
  359. x.GetParameters()[0].ParameterType == typeof(Stream) &&
  360. x.GetParameters()[1].ParameterType == typeof(StringEncoding) &&
  361. x.IsStatic == true &&
  362. x.ReturnType == T);
  363. if (DeserializeMethod3 != null) {
  364. return DeserializeMethod3.Invoke(null, new object[] { str, encode });
  365. } else if (DeserializeMethod1 != null) {
  366. return DeserializeMethod1.Invoke(null, new object[] { str });
  367. } else {
  368. if (T == typeof(string)) {
  369. string tmp = "";
  370. bool bEscaped = false;
  371. bool bRead = true;
  372. bool bUseQuotes = false;
  373. byte[] buf = new byte[1];
  374. MemoryStream mem = new MemoryStream();
  375. while(bRead) {
  376. str.Read(buf, 0, 1);
  377. if (buf[0] == 34 && mem.Length == 0 && bEscaped == false) {
  378. bUseQuotes = true;
  379. } else if (buf[0] == 92 && bEscaped == false) {
  380. bEscaped = true;
  381. } else if (mem.Length == 0 && (buf[0] == 9 || buf[0] == 32)) {
  382. // ignore space or tab at the beginning
  383. } else if (bUseQuotes && buf[0] == 34 && bEscaped == false) {
  384. // finish
  385. bRead = false;
  386. } else if (!bUseQuotes && mem.Length>0 && bEscaped == false && (buf[0]==32 || buf[0]==59 || buf[0]==44 || buf[0]==10 || buf[0]==13)) {
  387. // finish and undoo last position
  388. bRead = false;
  389. str.Position--;
  390. } else {
  391. mem.Write(buf,0,1);
  392. }
  393. bEscaped = false;
  394. }
  395. byte[] b = mem.ToArray();
  396. mem.Close();
  397. mem.Dispose();
  398. if (encode == StringEncoding.UTF8)
  399. tmp = Text.Encoding.UTF8.GetString(b);
  400. else if (encode == StringEncoding.UTF7)
  401. tmp = Text.Encoding.UTF7.GetString(b);
  402. else if (encode == StringEncoding.UTF32)
  403. tmp = Text.Encoding.UTF32.GetString(b);
  404. else if (encode == StringEncoding.Asci)
  405. tmp = Text.Encoding.ASCII.GetString(b);
  406. else
  407. tmp = Text.Encoding.Unicode.GetString(b);
  408. return tmp;
  409. } else if (T == typeof(System.Text.RegularExpressions.Regex)) {
  410. ushort opt = DeserializeObject<ushort>(str, encode);
  411. string pattern = DeserializeObject<string>(str, encode);
  412. return new Text.RegularExpressions.Regex(pattern, (Text.RegularExpressions.RegexOptions)opt);
  413. } else if (T == typeof(MemoryStream)) {
  414. //return new MemoryStream(DeserializeArray(str, typeof(byte[]), ArraySize.bits_32, StringEncoding.Asci));
  415. MemoryStream mem = new MemoryStream();
  416. uint length = DeserializeObject<uint>(str, encode);
  417. while (mem.Length < length) {
  418. int bytesToRead = 10000;
  419. if (length - mem.Length < 10000) {
  420. bytesToRead = (int)(length - mem.Length);
  421. }
  422. if (bytesToRead >= 1) {
  423. byte[] b = new byte[bytesToRead];
  424. str.Read(b, 0, bytesToRead);
  425. mem.Write(b, 0, bytesToRead);
  426. } else {
  427. break;
  428. }
  429. }
  430. } else if (T == typeof(Uri)) {
  431. string uri = DeserializeObject<string>(str, encode);
  432. return new Uri(uri);
  433. } else if (T == typeof(Image)) {
  434. return Image.FromStream(DeserializeObject<MemoryStream>(str, encode));
  435. } else if (T == typeof(Version)) {
  436. int major = DeserializeObject<int>(str, encode);
  437. int minor = DeserializeObject<int>(str, encode);
  438. int build = DeserializeObject<int>(str, encode);
  439. int rev = DeserializeObject<int>(str, encode);
  440. return new Version(major, minor, build, rev);
  441. } else if (T == typeof(Rectangle)) {
  442. int x = DeserializeObject<int>(str, encode);
  443. int y = DeserializeObject<int>(str, encode);
  444. int w = DeserializeObject<int>(str, encode);
  445. int h = DeserializeObject<int>(str, encode);
  446. return new System.Drawing.Rectangle(x, y, w, h);
  447. } else if (T == typeof(RectangleF)) {
  448. float x = DeserializeObject<float>(str, encode);
  449. float y = DeserializeObject<float>(str, encode);
  450. float w = DeserializeObject<float>(str, encode);
  451. float h = DeserializeObject<float>(str, encode);
  452. return new System.Drawing.RectangleF(x, y, w, h);
  453. } else if (T == typeof(Size)) {
  454. int w = DeserializeObject<int>(str, encode);
  455. int h = DeserializeObject<int>(str, encode);
  456. return new Size(w, h);
  457. } else if (T == typeof(SizeF)) {
  458. float w = DeserializeObject<float>(str, encode);
  459. float h = DeserializeObject<float>(str, encode);
  460. return new SizeF(w, h);
  461. } else if (T == typeof(Point)) {
  462. int x = DeserializeObject<int>(str, encode);
  463. int y = DeserializeObject<int>(str, encode);
  464. return new Point(x, y);
  465. } else if (T == typeof(PointF)) {
  466. float x = DeserializeObject<float>(str, encode);
  467. float y = DeserializeObject<float>(str, encode);
  468. return new PointF(x, y);
  469. } else if (T == typeof(Color)) {
  470. byte a = DeserializeObject<byte>(str, encode);
  471. byte r = DeserializeObject<byte>(str, encode);
  472. byte g = DeserializeObject<byte>(str, encode);
  473. byte b = DeserializeObject<byte>(str, encode);
  474. Color c = Color.FromArgb(a, r, g, b);
  475. } else if (T == typeof(Font)) {
  476. byte style = DeserializeObject<byte>(str, encode);
  477. float fsize = DeserializeObject<float>(str, encode);
  478. byte unit = DeserializeObject<byte>(str, encode);
  479. string FontFamily = DeserializeObject<string>(str, encode);
  480. return new Font(FontFamily, fsize, (FontStyle)style, (GraphicsUnit)unit);
  481. } else if (T == typeof(System.Globalization.CultureInfo)) {
  482. int culture_id = DeserializeObject<int>(str, encode);
  483. return new System.Globalization.CultureInfo(culture_id);
  484. } else if (T == typeof(bool)) {
  485. byte[] b = new byte[1];
  486. str.Read(b, 0, 1);
  487. return b[0] == 1;
  488. } else if (T == typeof(sbyte)) {
  489. byte[] b = new byte[1];
  490. str.Read(b, 0, 1);
  491. return (sbyte)(-128 + b[0]);
  492. } else if (T == typeof(char)) {
  493. byte[] b = new byte[2];
  494. str.Read(b, 0, 2);
  495. return BitConverter.ToChar(b, 0);
  496. } else if (T == typeof(decimal)) {
  497. int[] iNum = new int[4];
  498. iNum[0] = (int)DeserializeObject(str, typeof(int), encode);
  499. iNum[1] = (int)DeserializeObject(str, typeof(int), encode);
  500. iNum[2] = (int)DeserializeObject(str, typeof(int), encode);
  501. iNum[3] = (int)DeserializeObject(str, typeof(int), encode);
  502. //int[] iNum = (int[])DeserializeObject(str, typeof(int[]));
  503. return new decimal(iNum);
  504. } else if (T == typeof(DateTime)) {
  505. return new DateTime((long)DeserializeObject(str, typeof(long), encode));
  506. }
  507. if (typeinfo.IsArray) {
  508. return DeserializeArray(str, typeinfo.GetElementType(), encode);
  509. } else if (typeinfo.IsGenericType && typeinfo.GetGenericTypeDefinition() == typeof(List<>)) {
  510. return Enumerable.ToList(DeserializeArray(str, typeinfo.GetGenericArguments().First(), encode));
  511. }
  512. if (!T.IsClass) {
  513. byte[] buf = new byte[Marshal.SizeOf(T)];
  514. str.Read(buf, 0, buf.Length);
  515. return buf;
  516. }
  517. object ret = Activator.CreateInstance(typeinfo);
  518. foreach (var field in typeinfo.DeclaredFields.Where(x => (x.IsPublic || x.IsAssembly) && !x.IsStatic && x.GetCustomAttribute(typeof(Annotations.SerializeIgnore)) == null)) {
  519. field.SetValue(ret, DeserializeObject(str, field.FieldType, encode));
  520. }
  521. /*foreach(PropertyInfo pi in typeinfo.DeclaredProperties) {
  522. if(pi.CanRead) {
  523. dynamic d = DeserializeObject(str, pi.PropertyType, size, encode);
  524. if (pi.CanWrite)
  525. pi.SetValue(ret, d);
  526. }
  527. }*/
  528. return ret;
  529. }
  530. }
  531. public static void SerializeArray(string FilePath, Array array) {
  532. Stream str = new FileStream(FilePath, FileMode.Create);
  533. SerializeArray(str, array, DefaultStringEncoding);
  534. str.Close();
  535. }
  536. /** <summary>Do an array serialization. Used internally</summary> */
  537. public static void SerializeArray(Stream str, Array array) {
  538. SerializeArray(str, array, DefaultStringEncoding);
  539. }
  540. public static void SerializeArray(string FilePath, Array array, StringEncoding encode) {
  541. Stream str = new FileStream(FilePath, FileMode.Create);
  542. SerializeArray(str, array, encode);
  543. str.Close();
  544. }
  545. /** <summary>Do an array serialization. Used internally</summary> */
  546. public static void SerializeArray(Stream str, Array array, StringEncoding encode) {
  547. str.Write(new byte[] { 91 }, 0, 1);
  548. bool bFirst = true;
  549. foreach (var item in array) {
  550. if(bFirst == false)
  551. str.Write(new byte[] { 44 }, 0, 1);
  552. SerializeObject(str, item, encode);
  553. bFirst = false;
  554. }
  555. str.Write(new byte[] { 93 }, 0, 1);
  556. }
  557. /** <summary>Do an array deserialization. Used internally</summary> */
  558. public static dynamic DeserializeArray(string FilePath, Type ElemType) {
  559. if (File.Exists(FilePath)) {
  560. Stream str = new FileStream(FilePath, FileMode.Open);
  561. var res = DeserializeArray(str, ElemType, DefaultStringEncoding);
  562. str.Close();
  563. return res;
  564. }
  565. return createEmptyObject(ElemType);
  566. }
  567. /** <summary>Do an array deserialization. Used internally</summary> */
  568. public static dynamic DeserializeArray(Stream str, Type ElemType) {
  569. return DeserializeArray(str, ElemType, DefaultStringEncoding);
  570. }
  571. /** <summary>Do an array deserialization. Used internally</summary> */
  572. public static dynamic DeserializeArray(string FilePath, Type ElemType, StringEncoding encode) {
  573. if (File.Exists(FilePath)) {
  574. Stream str = new FileStream(FilePath, FileMode.Open);
  575. var res = DeserializeArray(str, ElemType, encode);
  576. str.Close();
  577. return res;
  578. }
  579. return createEmptyObject(ElemType);
  580. }
  581. /** <summary>Do an array deserialization. Used internally</summary> */
  582. public static dynamic DeserializeArray(Stream str, Type ElemType, StringEncoding encode) {
  583. dynamic ret;
  584. bool bReading = true;
  585. List<dynamic> res = new List<dynamic>();
  586. bool bInBrace = false;
  587. bool bEscaped = false;
  588. while (bReading) {
  589. long curPos = str.Position;
  590. byte[] bIsNull = new byte[1];
  591. str.Read(bIsNull, 0, 1);
  592. str.Position = curPos;
  593. if (bIsNull[0] == 92 && bEscaped == false) {
  594. bEscaped = true;
  595. } else {
  596. if (bInBrace) {
  597. // TODO: Lese komma-seperator (byte[44])
  598. if (bIsNull[0] == 93) {
  599. bReading = false;
  600. bInBrace = false;
  601. } else {
  602. dynamic tmp = Activator.CreateInstance(ElemType.MakeArrayType());
  603. tmp = (dynamic)DeserializeObject(str, ElemType, encode);
  604. res.Add(tmp);
  605. }
  606. } else {
  607. if (bIsNull[0] == 91) {
  608. bInBrace = true;
  609. } else if (bIsNull[0] == 9 || bIsNull[0] == 32) {
  610. // allowed character
  611. } else {
  612. // Error, not an array format
  613. }
  614. }
  615. }
  616. }
  617. ret = res.ToArray();
  618. return ret;
  619. }
  620. public static T DeserializeArray<T>(string FilePath) {
  621. if (File.Exists(FilePath)) {
  622. Stream str = new FileStream(FilePath, FileMode.Open);
  623. var res = DeserializeArray(str, typeof(T), DefaultStringEncoding);
  624. str.Close();
  625. return res;
  626. }
  627. return createEmptyObject<T>();
  628. }
  629. /** <summary>Do an array deserialization. Used internally</summary> */
  630. public static T DeserializeArray<T>(Stream str) {
  631. return DeserializeArray(str, typeof(T), DefaultStringEncoding);
  632. }
  633. /** <summary>Do an array deserialization. Used internally</summary> */
  634. public static T DeserializeArray<T>(string FilePath, StringEncoding encode) {
  635. if (File.Exists(FilePath)) {
  636. Stream str = new FileStream(FilePath, FileMode.Open);
  637. var res = DeserializeArray(str, typeof(T), encode);
  638. str.Close();
  639. return res;
  640. }
  641. return createEmptyObject<T>();
  642. }
  643. /** <summary>Do an array deserialization. Used internally</summary> */
  644. public static T DeserializeArray<T>(Stream str, StringEncoding encode) {
  645. return DeserializeArray(str, typeof(T), encode);
  646. }
  647. private static dynamic createEmptyObject(Type T) {
  648. TypeInfo typeinfo = T.GetTypeInfo();
  649. if (typeinfo.IsArray) {
  650. return Activator.CreateInstance(typeinfo, 0);
  651. } else {
  652. object empty_res = Activator.CreateInstance(typeinfo);
  653. return empty_res;
  654. }
  655. }
  656. private static T createEmptyObject<T>() {
  657. TypeInfo typeinfo = typeof(T).GetTypeInfo();
  658. T empty_res = (T)Activator.CreateInstance(typeinfo);
  659. return empty_res;
  660. }
  661. }
  662. }