binary-writer-method-gen.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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;
  8. public class Generator
  9. {
  10. public static void Main ()
  11. {
  12. Console.Out.NewLine = "\n";
  13. Type [] types = new Type [] {
  14. typeof (bool), typeof (DateTime), typeof (decimal), typeof (double),
  15. typeof (Guid), typeof (short), typeof (int), typeof (long), typeof (float), typeof (TimeSpan) };
  16. Dictionary<Type,byte> table = new Dictionary<Type,byte> ();
  17. // LAMESPEC: [MC-NBFX] section 2.3.3 dedscribes wrong RecordTypes.
  18. table.Add (typeof (bool), 0xB5);
  19. table.Add (typeof (short), 0x8B);
  20. table.Add (typeof (int), 0x8D);
  21. table.Add (typeof (long), 0x8F);
  22. table.Add (typeof (float), 0x91);
  23. table.Add (typeof (double), 0x93);
  24. table.Add (typeof (decimal), 0x95);
  25. table.Add (typeof (DateTime), 0x97);
  26. table.Add (typeof (TimeSpan), 0xAF);
  27. table.Add (typeof (Guid), 0xB1);
  28. Console.WriteLine (@"
  29. using System;
  30. using BF = System.Xml.XmlBinaryFormat;
  31. namespace System.Xml
  32. {
  33. internal partial class XmlBinaryDictionaryWriter : XmlDictionaryWriter
  34. {
  35. void CheckWriteArrayArguments (Array array, int offset, int length)
  36. {
  37. if (array == null)
  38. throw new ArgumentNullException (""array"");
  39. if (offset < 0)
  40. throw new ArgumentOutOfRangeException (""offset is negative"");
  41. if (offset > array.Length)
  42. throw new ArgumentOutOfRangeException (""offset exceeds the length of the destination array"");
  43. if (length < 0)
  44. throw new ArgumentOutOfRangeException (""length is negative"");
  45. if (length > array.Length - offset)
  46. throw new ArgumentOutOfRangeException (""length + offset exceeds the length of the destination array"");
  47. }
  48. void CheckDictionaryStringArgs (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  49. {
  50. if (localName == null)
  51. throw new ArgumentNullException (""localName"");
  52. if (namespaceUri == null)
  53. throw new ArgumentNullException (""namespaceUri"");
  54. }
  55. ");
  56. foreach (Type type in types) {
  57. Console.WriteLine (@"
  58. public override void WriteArray (string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, {0} [] array, int offset, int length)
  59. {{
  60. CheckDictionaryStringArgs (localName, namespaceUri);
  61. writer.Write (BF.Array);
  62. WriteStartElement (prefix, localName, namespaceUri);
  63. WriteEndElement ();
  64. WriteArrayRemaining (array, offset, length);
  65. }}
  66. public override void WriteArray (string prefix, string localName, string namespaceUri, {0} [] array, int offset, int length)
  67. {{
  68. CheckWriteArrayArguments (array, offset, length);
  69. writer.Write (BF.Array);
  70. WriteStartElement (prefix, localName, namespaceUri);
  71. WriteEndElement ();
  72. WriteArrayRemaining (array, offset, length);
  73. }}
  74. void WriteArrayRemaining ({0} [] array, int offset, int length)
  75. {{
  76. writer.Write ((byte) 0x{2:X02}); // ident
  77. writer.WriteFlexibleInt (length);
  78. for (int i = offset; i < offset + length; i++)
  79. WriteValueContent (array [i]);
  80. }}", ToCSharp (type), type.Name, table [type]);
  81. // <note>
  82. // WriteArrayRemaining() is generated, but are modified and moved into
  83. // XmlBinaryDictionaryWriter. (I keep it open here so that we
  84. // make sure to remove this before compiling. Remove this to get
  85. // it working fine).
  86. // </note>
  87. }
  88. Console.WriteLine (@"
  89. }
  90. }");
  91. }
  92. static CodeDomProvider cs = new CSharpCodeProvider ();
  93. static string ToCSharp (Type type)
  94. {
  95. string r = cs.GetTypeOutput (new CodeTypeReference (type));
  96. return r != type.FullName ? r : type.Name;
  97. }
  98. }