New test.
[mono.git] / mcs / tools / misc / serialize.cs
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
16 using System;
17 using System.Collections;
18 using System.Globalization;
19 using System.Runtime.Serialization;
20 using System.Runtime.Serialization.Formatters.Soap;
21 using System.IO;
22
23 namespace Mono.Serialize {
24         class Driver {
25                 static object StaticCreateObject ()
26                 {
27                         //
28                         // Change the object type here.
29                         //
30                         return null;
31                 }
32
33                 static object LiveCreateObject (Type obj, Type[] types, string[] values)
34                 {
35                         if (types.Length != values.Length)
36                                 throw new ArgumentException ();
37                         
38                         object[] a = new object [types.Length];
39                         
40                         for (int i = 0; i < a.Length; i++)
41                                 a [i] = Convert.ChangeType (values [i], types [i]);
42                         
43                         return Activator.CreateInstance (obj, a);
44                 }
45         
46                 static void Main (string[] args)
47                 {
48                         object x = null;
49                         string strTypes = null;
50                         string argValues = null;
51                         
52                         if (args.Length == 1) {
53                                 Type t = Type.GetType (args[0]);
54                                 Console.WriteLine ("\nPlease enter the arguments to the constructor for type {0}", t.ToString());
55                                 strTypes = Console.ReadLine ();
56                                 Console.WriteLine ("\nPlease enter the values");
57                                 argValues = Console.ReadLine ();
58                                 Type[] types = ToTypeArray (strTypes.Split (','));
59                                 string[] param = argValues.Split (',');
60                                 
61                                 x = LiveCreateObject (t, types, param);
62                         } else {
63                                 x = StaticCreateObject ();
64                         }
65                         
66                         string fileName = x.GetType().FullName + ".xml";
67                         Stream output = new FileStream (fileName, FileMode.Create,
68                                                         FileAccess.Write, FileShare.None);
69                         IFormatter formatter = new SoapFormatter ();
70                         
71                         formatter.Serialize ((Stream) output, x);
72                         output.Close ();
73                 }
74                 
75                 public static Type[] ToTypeArray (string[] strTypes)
76                 {
77                         Type[] t = new Type [strTypes.Length];
78                         
79                         for (int i = 0; i < strTypes.Length; i++)
80                                 t [i] = StringToType (strTypes [i]);
81                         return t;
82                 }
83                 
84                 public static Type StringToType (string s)
85                 {
86                         switch (s) {
87                         case "bool":
88                                 return typeof (System.Boolean);
89                                 break;
90                         case "byte":
91                                 return typeof (System.Byte);
92                                 break;
93                         case "sbyte":
94                                 return typeof (System.SByte);
95                                 break;
96                         case "char":
97                                 return typeof (System.Char);
98                                 break;
99                         case "decimal":
100                                 return typeof (System.Decimal);
101                                 break;
102                         case "double":
103                                 return typeof (System.Double);
104                                 break;
105                         case "float":
106                                 return typeof (System.Single);
107                                 break;
108                         case "int":
109                                 return typeof (System.Int32);
110                                 break;
111                         case "uint":
112                                 return typeof (System.UInt32);
113                                 break;
114                         case "long":
115                                 return typeof (System.Int64);
116                                 break;
117                         case "ulong":
118                                 return typeof (System.UInt64);
119                                 break;
120                         case "object":
121                                 return typeof (System.Object);
122                                 break;
123                         case "short":
124                                 return typeof (System.Int16);
125                                 break;
126                         case "ushort":
127                                 return typeof (System.UInt16);
128                                 break;
129                         case "string":
130                                 return typeof (System.String);
131                                 break;
132                         default:
133                                 return Type.GetType (s);
134                                 break;
135                         }
136                 }
137         }
138 }