2010-04-15 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Runtime.Serialization / binary-writer-method-gen.cs
blobdf3e5d9a20dc6567534819bf5bf5419049e7d8c7
1 // mono binary-writer-method-gen.exe > System.Xml/XmlBinaryDictionaryWriterAutoGen.cs
2 using System;
3 using System.Collections.Generic;
4 using System.Globalization;
5 using System.CodeDom;
6 using System.CodeDom.Compiler;
7 using Microsoft.CSharp;
9 public class Generator
11 public static void Main ()
13 Console.Out.NewLine = "\n";
14 Type [] types = new Type [] {
15 typeof (bool), typeof (DateTime), typeof (decimal), typeof (double),
16 typeof (Guid), typeof (short), typeof (int), typeof (long), typeof (float), typeof (TimeSpan) };
18 Dictionary<Type,byte> table = new Dictionary<Type,byte> ();
19 // LAMESPEC: [MC-NBFX] section 2.3.3 dedscribes wrong RecordTypes.
20 table.Add (typeof (bool), 0xB5);
21 table.Add (typeof (short), 0x8B);
22 table.Add (typeof (int), 0x8D);
23 table.Add (typeof (long), 0x8F);
24 table.Add (typeof (float), 0x91);
25 table.Add (typeof (double), 0x93);
26 table.Add (typeof (decimal), 0x95);
27 table.Add (typeof (DateTime), 0x97);
28 table.Add (typeof (TimeSpan), 0xAF);
29 table.Add (typeof (Guid), 0xB1);
31 Console.WriteLine (@"
32 using System;
33 using BF = System.Xml.XmlBinaryFormat;
35 namespace System.Xml
37 internal partial class XmlBinaryDictionaryWriter : XmlDictionaryWriter
39 void CheckWriteArrayArguments (Array array, int offset, int length)
41 if (array == null)
42 throw new ArgumentNullException (""array"");
43 if (offset < 0)
44 throw new ArgumentOutOfRangeException (""offset is negative"");
45 if (offset > array.Length)
46 throw new ArgumentOutOfRangeException (""offset exceeds the length of the destination array"");
47 if (length < 0)
48 throw new ArgumentOutOfRangeException (""length is negative"");
49 if (length > array.Length - offset)
50 throw new ArgumentOutOfRangeException (""length + offset exceeds the length of the destination array"");
53 void CheckDictionaryStringArgs (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
55 if (localName == null)
56 throw new ArgumentNullException (""localName"");
57 if (namespaceUri == null)
58 throw new ArgumentNullException (""namespaceUri"");
60 ");
62 foreach (Type type in types) {
63 Console.WriteLine (@"
65 public override void WriteArray (string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, {0} [] array, int offset, int length)
67 CheckDictionaryStringArgs (localName, namespaceUri);
68 writer.Write (BF.Array);
69 WriteStartElement (prefix, localName, namespaceUri);
70 WriteEndElement ();
71 WriteArrayRemaining (array, offset, length);
74 public override void WriteArray (string prefix, string localName, string namespaceUri, {0} [] array, int offset, int length)
76 CheckWriteArrayArguments (array, offset, length);
77 writer.Write (BF.Array);
78 WriteStartElement (prefix, localName, namespaceUri);
79 WriteEndElement ();
80 WriteArrayRemaining (array, offset, length);
83 void WriteArrayRemaining ({0} [] array, int offset, int length)
85 writer.Write ((byte) 0x{2:X02}); // ident
86 writer.WriteFlexibleInt (length);
87 for (int i = offset; i < offset + length; i++)
88 WriteValueContent (array [i]);
89 }}", ToCSharp (type), type.Name, table [type]);
91 // <note>
92 // WriteArrayRemaining() is generated, but are modified and moved into
93 // XmlBinaryDictionaryWriter. (I keep it open here so that we
94 // make sure to remove this before compiling. Remove this to get
95 // it working fine).
96 // </note>
100 Console.WriteLine (@"
102 }");
105 static CodeDomProvider cs = new CSharpCodeProvider ();
107 static string ToCSharp (Type type)
109 string r = cs.GetTypeOutput (new CodeTypeReference (type));
110 return r != type.FullName ? r : type.Name;