writer-method-gen.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // mono writer-method-gen.exe > System.Xml/XmlDictionaryWriterAutoGen.cs
  2. using System;
  3. using System.Globalization;
  4. using System.CodeDom;
  5. using System.CodeDom.Compiler;
  6. using Microsoft.CSharp;
  7. public class Generator
  8. {
  9. public static void Main ()
  10. {
  11. Console.Out.NewLine = "\n";
  12. Type [] types = new Type [] {
  13. typeof (bool), typeof (DateTime), typeof (decimal), typeof (double),
  14. typeof (Guid), typeof (short), typeof (int), typeof (long), typeof (float), typeof (TimeSpan) };
  15. Console.WriteLine (@"
  16. using System;
  17. namespace System.Xml
  18. {
  19. public abstract partial class XmlDictionaryWriter : XmlWriter
  20. {
  21. void CheckWriteArrayArguments (Array array, int offset, int length)
  22. {
  23. if (array == null)
  24. throw new ArgumentNullException (""array"");
  25. if (offset < 0)
  26. throw new ArgumentOutOfRangeException (""offset is negative"");
  27. if (offset > array.Length)
  28. throw new ArgumentOutOfRangeException (""offset exceeds the length of the destination array"");
  29. if (length < 0)
  30. throw new ArgumentOutOfRangeException (""length is negative"");
  31. if (length > array.Length - offset)
  32. throw new ArgumentOutOfRangeException (""length + offset exceeds the length of the destination array"");
  33. }
  34. void CheckDictionaryStringArgs (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  35. {
  36. if (localName == null)
  37. throw new ArgumentNullException (""localName"");
  38. if (namespaceUri == null)
  39. throw new ArgumentNullException (""namespaceUri"");
  40. }
  41. ");
  42. foreach (Type type in types) {
  43. Console.WriteLine (@"
  44. public virtual void WriteArray (string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, {0} [] array, int offset, int length)
  45. {{
  46. CheckDictionaryStringArgs (localName, namespaceUri);
  47. WriteArray (prefix, localName.Value, namespaceUri.Value, array, offset, length);
  48. }}
  49. public virtual void WriteArray (string prefix, string localName, string namespaceUri, {0} [] array, int offset, int length)
  50. {{
  51. CheckWriteArrayArguments (array, offset, length);
  52. for (int i = 0; i < length; i++) {{
  53. WriteStartElement (prefix, localName, namespaceUri);
  54. WriteValue (array [offset + i]);
  55. WriteEndElement ();
  56. }}
  57. }}", ToCSharp (type), type.Name);
  58. }
  59. Console.WriteLine (@"
  60. }
  61. }");
  62. }
  63. static CodeDomProvider cs = new CSharpCodeProvider ();
  64. static string ToCSharp (Type type)
  65. {
  66. string r = cs.GetTypeOutput (new CodeTypeReference (type));
  67. return r != type.FullName ? r : type.Name;
  68. }
  69. }