4 // This program creates a SerializationInfo and requests an object
5 // to serialize itself.
7 // We serialize because we need to know the *exact* names that are
8 // used for the values being serialized.
10 // Author: Miguel de Icaza
17 using System
.Collections
;
18 using System
.Globalization
;
19 using System
.Runtime
.Serialization
;
20 using System
.Runtime
.Serialization
.Formatters
.Soap
;
23 namespace Mono
.Serialize
{
25 static object StaticCreateObject ()
28 // Change the object type here.
33 static object LiveCreateObject (Type obj
, Type
[] types
, string[] values
)
35 if (types
.Length
!= values
.Length
)
36 throw new ArgumentException ();
38 object[] a
= new object [types
.Length
];
40 for (int i
= 0; i
< a
.Length
; i
++)
41 a
[i
] = Convert
.ChangeType (values
[i
], types
[i
]);
43 return Activator
.CreateInstance (obj
, a
);
46 static void Main (string[] args
)
49 string strTypes
= null;
50 string argValues
= null;
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 (',');
61 x
= LiveCreateObject (t
, types
, param
);
63 x
= StaticCreateObject ();
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 ();
71 formatter
.Serialize ((Stream
) output
, x
);
75 public static Type
[] ToTypeArray (string[] strTypes
)
77 Type
[] t
= new Type
[strTypes
.Length
];
79 for (int i
= 0; i
< strTypes
.Length
; i
++)
80 t
[i
] = StringToType (strTypes
[i
]);
84 public static Type
StringToType (string s
)
88 return typeof (System
.Boolean
);
91 return typeof (System
.Byte
);
94 return typeof (System
.SByte
);
97 return typeof (System
.Char
);
100 return typeof (System
.Decimal
);
103 return typeof (System
.Double
);
106 return typeof (System
.Single
);
109 return typeof (System
.Int32
);
112 return typeof (System
.UInt32
);
115 return typeof (System
.Int64
);
118 return typeof (System
.UInt64
);
121 return typeof (System
.Object
);
124 return typeof (System
.Int16
);
127 return typeof (System
.UInt16
);
130 return typeof (System
.String
);
133 return Type
.GetType (s
);