serialize.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Serialize.cs
  3. //
  4. // This program creates a SerializationInfo and requests an object
  5. // to serialize itself.
  6. //
  7. // We serialize because we need to know the *exact* names that are
  8. // used for the values being serialized.
  9. //
  10. // Author: Miguel de Icaza
  11. // Duncan Mak
  12. //
  13. // (C) Ximian, Inc.
  14. //
  15. using System;
  16. using System.Collections;
  17. using System.Globalization;
  18. using System.Runtime.Serialization;
  19. using System.Runtime.Serialization.Formatters.Soap;
  20. using System.IO;
  21. namespace Mono.Serialize {
  22. class Driver {
  23. static object StaticCreateObject ()
  24. {
  25. //
  26. // Change the object type here.
  27. //
  28. return null;
  29. }
  30. static object LiveCreateObject (Type obj, Type[] types, string[] values)
  31. {
  32. if (types.Length != values.Length)
  33. throw new ArgumentException ();
  34. object[] a = new object [types.Length];
  35. for (int i = 0; i < a.Length; i++)
  36. a [i] = Convert.ChangeType (values [i], types [i]);
  37. return Activator.CreateInstance (obj, a);
  38. }
  39. static void Main (string[] args)
  40. {
  41. object x = null;
  42. string strTypes = null;
  43. string argValues = null;
  44. if (args.Length == 1) {
  45. Type t = Type.GetType (args[0]);
  46. Console.WriteLine ("\nPlease enter the arguments to the constructor for type {0}", t.ToString());
  47. strTypes = Console.ReadLine ();
  48. Console.WriteLine ("\nPlease enter the values");
  49. argValues = Console.ReadLine ();
  50. Type[] types = ToTypeArray (strTypes.Split (','));
  51. string[] param = argValues.Split (',');
  52. x = LiveCreateObject (t, types, param);
  53. } else {
  54. x = StaticCreateObject ();
  55. }
  56. string fileName = x.GetType().FullName + ".xml";
  57. Stream output = new FileStream (fileName, FileMode.Create,
  58. FileAccess.Write, FileShare.None);
  59. IFormatter formatter = new SoapFormatter ();
  60. formatter.Serialize ((Stream) output, x);
  61. output.Close ();
  62. }
  63. public static Type[] ToTypeArray (string[] strTypes)
  64. {
  65. Type[] t = new Type [strTypes.Length];
  66. for (int i = 0; i < strTypes.Length; i++)
  67. t [i] = StringToType (strTypes [i]);
  68. return t;
  69. }
  70. public static Type StringToType (string s)
  71. {
  72. switch (s) {
  73. case "bool":
  74. return typeof (System.Boolean);
  75. break;
  76. case "byte":
  77. return typeof (System.Byte);
  78. break;
  79. case "sbyte":
  80. return typeof (System.SByte);
  81. break;
  82. case "char":
  83. return typeof (System.Char);
  84. break;
  85. case "decimal":
  86. return typeof (System.Decimal);
  87. break;
  88. case "double":
  89. return typeof (System.Double);
  90. break;
  91. case "float":
  92. return typeof (System.Single);
  93. break;
  94. case "int":
  95. return typeof (System.Int32);
  96. break;
  97. case "uint":
  98. return typeof (System.UInt32);
  99. break;
  100. case "long":
  101. return typeof (System.Int64);
  102. break;
  103. case "ulong":
  104. return typeof (System.UInt64);
  105. break;
  106. case "object":
  107. return typeof (System.Object);
  108. break;
  109. case "short":
  110. return typeof (System.Int16);
  111. break;
  112. case "ushort":
  113. return typeof (System.UInt16);
  114. break;
  115. case "string":
  116. return typeof (System.String);
  117. break;
  118. default:
  119. return Type.GetType (s);
  120. break;
  121. }
  122. }
  123. }
  124. }