1 // mono reader-method-gen.exe > System.Xml/XmlDictionaryReaderAutoGen.cs
3 using System.Globalization;
5 using System.CodeDom.Compiler;
6 using System.Reflection;
8 using Microsoft.CSharp;
10 public class Generator
12 public static void Main ()
14 Console.Out.NewLine = "\n";
15 Type [] types = new Type [] {
16 typeof (bool), typeof (DateTime), typeof (decimal), typeof (double),
17 typeof (Guid), typeof (short), typeof (int), typeof (long), typeof (float), typeof (TimeSpan) };
20 #pragma warning disable 612
22 using System.Collections.Generic;
26 public abstract partial class XmlDictionaryReader : XmlReader
28 static readonly char [] wsChars = new char [] {' ', '\t', '\n', '\r'};
30 void CheckReadArrayArguments (Array array, int offset, int length)
33 throw new ArgumentNullException (""array"");
35 throw new ArgumentOutOfRangeException (""offset is negative"");
36 if (offset > array.Length)
37 throw new ArgumentOutOfRangeException (""offset exceeds the length of the destination array"");
39 throw new ArgumentOutOfRangeException (""length is negative"");
40 if (length > array.Length - offset)
41 throw new ArgumentOutOfRangeException (""length + offset exceeds the length of the destination array"");
44 void CheckDictionaryStringArgs (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
46 if (localName == null)
47 throw new ArgumentNullException (""localName"");
48 if (namespaceUri == null)
49 throw new ArgumentNullException (""namespaceUri"");
53 foreach (Type type in types) {
55 public virtual int ReadArray (XmlDictionaryString localName, XmlDictionaryString namespaceUri, {0} [] array, int offset, int length)
57 CheckDictionaryStringArgs (localName, namespaceUri);
58 return ReadArray (localName.Value, namespaceUri.Value, array, offset, length);
61 public virtual int ReadArray (string localName, string namespaceUri, {0} [] array, int offset, int length)
63 CheckReadArrayArguments (array, offset, length);
64 for (int i = 0; i < length; i++) {{
66 if (NodeType != XmlNodeType.Element)
68 ReadStartElement (localName, namespaceUri);
69 array [offset + i] = XmlConvert.To{1} (ReadContentAsString ());
75 public virtual {0} [] Read{1}Array (string localName, string namespaceUri)
77 List<{0}> list = new List<{0}> ();
80 if (NodeType != XmlNodeType.Element)
82 ReadStartElement (localName, namespaceUri);
83 list.Add (XmlConvert.To{1} (ReadContentAsString ()));
85 if (list.Count == Quotas.MaxArrayLength)
86 // FIXME: check if raises an error or not
89 return list.ToArray ();
92 public virtual {0} [] Read{1}Array (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
94 CheckDictionaryStringArgs (localName, namespaceUri);
95 return Read{1}Array (localName.Value, namespaceUri.Value);
96 }}", ToCSharp (type), type.Name);
100 Type xr = typeof (XmlReader);
101 string name = "ReadElementContentAs";
102 foreach (MethodInfo mi in xr.GetMethods ()) {
103 if (!mi.Name.StartsWith (name))
105 ParameterInfo [] pl = mi.GetParameters ();
106 if (pl.Length != 2 || pl [0].ParameterType != typeof (string))
108 if (mi.Name.EndsWith ("AsObject"))
109 continue; // special case to filter out.
110 if (mi.Name.EndsWith ("AsString"))
111 continue; // special case to filter out.
113 bool isOverride = xr.GetMethod (mi.Name, Type.EmptyTypes) != null;
114 Console.WriteLine (@"
117 ReadStartElement (LocalName, NamespaceURI);
122 ToCSharp (mi.ReturnType),
124 mi.Name.Replace ("Element", String.Empty),
125 isOverride ? "override " : null);
128 Console.WriteLine (@"
133 static CodeDomProvider cs = new CSharpCodeProvider ();
135 static string ToCSharp (Type type)
137 string r = cs.GetTypeOutput (new CodeTypeReference (type));
138 return r != type.FullName ? r : type.Name;
141 static string ToOldName (Type type)