| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- //
- // Mono Class Libraries
- // System.Xml.Serialization.XmlSerializer
- //
- // Authors:
- // John Donagher ([email protected])
- // Ajay kumar Dwivedi ([email protected])
- // Tim Coleman ([email protected])
- //
- // (C) 2002 John Donagher, Ajay kumar Dwivedi
- // Copyright (C) Tim Coleman, 2002
- //
- using System;
- using System.Collections;
- using System.IO;
- using System.Reflection;
- using System.Xml;
- using System.Xml.Schema;
- namespace System.Xml.Serialization {
- /// <summary>
- /// Summary description for XmlSerializer.
- /// </summary>
- public class XmlSerializer {
- #region Fields
- Type type;
- XmlAttributeOverrides overrides;
- Type[] extraTypes;
- XmlRootAttribute rootAttribute;
- string defaultNamespace;
- Hashtable typeTable;
- bool useOrder;
- bool isNullable;
- Hashtable typeMappings = new Hashtable ();
- #endregion // Fields
- #region Constructors
- protected XmlSerializer ()
- {
- }
- public XmlSerializer (Type type)
- : this (type, null, null, null, null)
- {
- }
- public XmlSerializer (XmlTypeMapping xmlTypeMapping)
- {
- typeMappings.Add (xmlTypeMapping.TypeFullName, xmlTypeMapping);
- }
- public XmlSerializer (Type type, string defaultNamespace)
- : this (type, null, null, null, defaultNamespace)
- {
- }
- public XmlSerializer (Type type, Type[] extraTypes)
- : this (type, null, extraTypes, null, null)
- {
- }
- public XmlSerializer (Type type, XmlAttributeOverrides overrides)
- : this (type, overrides, null, null, null)
- {
- }
- public XmlSerializer (Type type, XmlRootAttribute root)
- : this (type, null, null, root, null)
- {
- }
- internal XmlSerializer (Hashtable typeTable)
- {
- this.typeTable = typeTable;
- }
- public XmlSerializer (Type type,
- XmlAttributeOverrides overrides,
- Type [] extraTypes,
- XmlRootAttribute root,
- string defaultNamespace)
- {
- if (type == null)
- throw new ArgumentNullException ("type");
- XmlReflectionImporter ri = new XmlReflectionImporter (overrides, defaultNamespace);
- TypeData td = TypeTranslator.GetTypeData (type);
- typeMappings.Add (td.FullTypeName, ri.ImportTypeMapping (type, root, defaultNamespace));
- ri.IncludeTypes (type);
- if (extraTypes != null) {
- foreach (Type t in extraTypes) {
- td = TypeTranslator.GetTypeData (t);
- string n = td.FullTypeName;
- typeMappings.Add (n, ri.ImportTypeMapping (type, root, defaultNamespace));
- ri.IncludeTypes (t);
- }
- }
-
- this.type = type;
- this.overrides = overrides;
- this.extraTypes = (extraTypes == null ? new Type[0] : extraTypes);
-
- if (root != null)
- this.rootAttribute = root;
- else {
- object[] attributes = type.GetCustomAttributes (typeof (XmlRootAttribute), false);
- if (attributes.Length > 0)
- this.rootAttribute = (XmlRootAttribute) attributes[0];
- }
-
- this.defaultNamespace = defaultNamespace;
- if (typeTable == null)
- typeTable = new Hashtable ();
- FillTypeTable (type);
- }
- #endregion // Constructors
- #region Events
- public event XmlAttributeEventHandler UnknownAttribute;
- public event XmlElementEventHandler UnknownElement;
- public event XmlNodeEventHandler UnknownNode;
- public event UnreferencedObjectEventHandler UnreferencedObject;
- #endregion // Events
- #region Properties
- internal bool UseOrder {
- get { return useOrder; }
- set { useOrder = value; }
- }
- #endregion // Properties
- #region Methods
- [MonoTODO]
- public virtual bool CanDeserialize (XmlReader xmlReader)
- {
- throw new NotImplementedException ();
- }
- protected virtual XmlSerializationReader CreateReader ()
- {
- // This is what MS does!!!
- throw new NotImplementedException ();
- }
- protected virtual XmlSerializationWriter CreateWriter ()
- {
- // This is what MS does!!!
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public object Deserialize (Stream stream)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public object Deserialize (TextReader textReader)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public object Deserialize (XmlReader xmlReader)
- {
- throw new NotImplementedException ();
- }
- protected virtual object Deserialize (XmlSerializationReader reader)
- {
- // This is what MS does!!!
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static XmlSerializer [] FromMappings (XmlMapping [] mappings)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static XmlSerializer [] FromTypes (Type [] mappings)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- protected virtual void Serialize (object o, XmlSerializationWriter writer)
- {
- throw new NotImplementedException ();
- }
- public void Serialize (Stream stream, object o)
- {
- XmlTextWriter xmlWriter = new XmlTextWriter (stream, System.Text.Encoding.Default);
- xmlWriter.Formatting = Formatting.Indented;
- Serialize (xmlWriter, o, null);
- }
-
- public void Serialize (TextWriter textWriter, object o)
- {
- XmlTextWriter xmlWriter = new XmlTextWriter (textWriter);
- xmlWriter.Formatting = Formatting.Indented;
- Serialize (xmlWriter, o, null);
- }
-
- public void Serialize (XmlWriter xmlWriter, object o)
- {
- Serialize (xmlWriter, o, null);
- }
-
- public void Serialize (Stream stream, object o, XmlSerializerNamespaces namespaces)
- {
- XmlTextWriter xmlWriter = new XmlTextWriter (stream, System.Text.Encoding.Default);
- xmlWriter.Formatting = Formatting.Indented;
- Serialize (xmlWriter, o, namespaces);
- }
-
- public void Serialize (TextWriter textWriter, object o, XmlSerializerNamespaces namespaces)
- {
- XmlTextWriter xmlWriter = new XmlTextWriter (textWriter);
- xmlWriter.Formatting = Formatting.Indented;
- Serialize (xmlWriter, o, namespaces);
- }
- public void Serialize (XmlWriter writer, object o, XmlSerializerNamespaces namespaces)
- {
- Type objType = o.GetType ();
- string rootName = objType.Name;
- string rootNs = String.Empty;
- string rootPrefix = String.Empty;
- bool isBuiltIn = IsInbuiltType (objType);
- if (namespaces == null)
- namespaces = new XmlSerializerNamespaces ();
- if (namespaces.Count == 0) {
- namespaces.Add ("xsd", XmlSchema.Namespace);
- namespaces.Add ("xsi", XmlSchema.InstanceNamespace);
- }
- XmlSerializerNamespaces nss = new XmlSerializerNamespaces ();
- XmlQualifiedName[] qnames;
- if (isBuiltIn) {
- writer.WriteStartDocument ();
- SerializeBuiltIn (writer, o);
- writer.WriteEndDocument ();
- return;
- }
-
- writer.WriteStartDocument ();
- object [] memberObj = (object []) typeTable [objType];
- if (memberObj == null)
- throw new Exception ("Unknown Type " + objType +
- " encountered during Serialization");
- Hashtable memberTable = (Hashtable) memberObj [0];
- XmlAttributes xmlAttributes = (XmlAttributes) memberTable [""];
- //If we have been passed an XmlRoot, set it on the base class
- if (rootAttribute != null)
- xmlAttributes.XmlRoot = rootAttribute;
-
- if (xmlAttributes.XmlRoot != null) {
- isNullable = xmlAttributes.XmlRoot.IsNullable;
- if (xmlAttributes.XmlRoot.ElementName != null)
- rootName = xmlAttributes.XmlRoot.ElementName;
- rootNs = xmlAttributes.XmlRoot.Namespace;
- }
- if (namespaces != null && namespaces.GetPrefix (rootNs) != null)
- rootPrefix = namespaces.GetPrefix (rootNs);
- //XMLNS attributes in the Root
- XmlAttributes XnsAttrs = (XmlAttributes) ((object[]) typeTable[objType])[1];
- if (XnsAttrs != null) {
- MemberInfo member = XnsAttrs.MemberInfo;
- FieldInfo fieldInfo = member as FieldInfo;
- PropertyInfo propertyInfo = member as PropertyInfo;
- XmlSerializerNamespaces xns;
- if (fieldInfo != null)
- xns = (XmlSerializerNamespaces) fieldInfo.GetValue (o);
- else
- xns = (XmlSerializerNamespaces) propertyInfo.GetValue (o, null);
-
- qnames = xns.ToArray ();
- foreach (XmlQualifiedName qname in qnames)
- nss.Add (qname.Name, qname.Namespace);
- }
- //XmlNs from the namespaces passed
- qnames = namespaces.ToArray ();
- foreach (XmlQualifiedName qname in qnames)
- if (writer.LookupPrefix (qname.Namespace) != qname.Name)
- nss.Add (qname.Name, qname.Namespace);
- writer.WriteStartElement (rootPrefix, rootName, rootNs);
- qnames = nss.ToArray();
- foreach (XmlQualifiedName qname in qnames)
- if (writer.LookupPrefix (qname.Namespace) != qname.Name)
- writer.WriteAttributeString ("xmlns", qname.Name, null, qname.Namespace);
- if (rootPrefix == String.Empty && rootNs != String.Empty && rootNs != null)
- writer.WriteAttributeString (String.Empty, "xmlns", null, rootNs);
- SerializeMembers (writer, o, true);//, namespaces);
- writer.WriteEndDocument ();
- }
- private void SerializeBuiltIn (XmlWriter writer, object o)
- {
- TypeData td = TypeTranslator.GetTypeData (o.GetType ());
- writer.WriteElementString (td.ElementName, o.ToString ());
- }
- private void SerializeMembers (XmlWriter writer, object o, bool isRoot)
- {
- Type objType = o.GetType ();
- XmlAttributes nsAttributes = (XmlAttributes) ((object[]) typeTable [objType])[1];
- ArrayList attributes = (ArrayList) ((object[]) typeTable [objType])[2];
- ArrayList elements = (ArrayList) ((object[]) typeTable [objType])[3];
- if (!isRoot && nsAttributes != null) {
- MemberInfo member = nsAttributes.MemberInfo;
- FieldInfo fieldInfo = member as FieldInfo;
- PropertyInfo propertyInfo = member as PropertyInfo;
- XmlSerializerNamespaces xns;
- if (fieldInfo != null)
- xns = (XmlSerializerNamespaces) fieldInfo.GetValue (o);
- else
- xns = (XmlSerializerNamespaces) propertyInfo.GetValue (o, null);
-
- XmlQualifiedName[] qnames = xns.ToArray ();
- foreach (XmlQualifiedName qname in qnames)
- if (writer.LookupPrefix (qname.Namespace) != qname.Name)
- writer.WriteAttributeString ("xmlns", qname.Name, null, qname.Namespace);
- }
- //Serialize the Attributes.
- foreach (XmlAttributes xmlAttributes in attributes) {
- MemberInfo member = xmlAttributes.MemberInfo;
- FieldInfo fieldInfo = member as FieldInfo;
- PropertyInfo propertyInfo = member as PropertyInfo;
- Type attributeType;
- object attributeValue;
- string attributeValueString;
- string attributeName;
- string attributeNs;
- if (fieldInfo != null) {
- attributeType = fieldInfo.FieldType;
- attributeValue = fieldInfo.GetValue (o);
- }
- else {
- attributeType = propertyInfo.PropertyType;
- attributeValue = propertyInfo.GetValue (o, null);
- }
- attributeName = xmlAttributes.GetAttributeName (attributeType, member.Name);
- attributeNs = xmlAttributes.GetAttributeNamespace (attributeType);
-
- if (attributeValue is XmlQualifiedName) {
- XmlQualifiedName qname = (XmlQualifiedName) attributeValue;
- if (qname.IsEmpty)
- continue;
- writer.WriteStartAttribute (attributeName, attributeNs);
- writer.WriteQualifiedName (qname.Name, qname.Namespace);
- writer.WriteEndAttribute ();
- continue;
- }
- else if (attributeValue is XmlQualifiedName[]) {
- XmlQualifiedName[] qnames = (XmlQualifiedName[]) attributeValue;
- writer.WriteStartAttribute (attributeName, attributeNs);
- int count = 0;
- foreach (XmlQualifiedName qname in qnames) {
- if (qname.IsEmpty)
- continue;
- if (count++ > 0)
- writer.WriteWhitespace (" ");
- writer.WriteQualifiedName (qname.Name, qname.Namespace);
- }
- writer.WriteEndAttribute ();
- continue;
- }
- else if (attributeValue is XmlAttribute[]) {
- XmlAttribute[] xmlattrs = (XmlAttribute[]) attributeValue;
- foreach (XmlAttribute xmlattr in xmlattrs)
- xmlattr.WriteTo(writer);
- continue;
- }
- attributeValueString = GetXmlValue (attributeValue);
- if (attributeValueString != GetXmlValue (xmlAttributes.XmlDefaultValue))
- writer.WriteAttributeString (attributeName, attributeNs, attributeValueString);
- }
- // Serialize Elements
- foreach (XmlAttributes xmlElements in elements) {
- MemberInfo member = xmlElements.MemberInfo;
- FieldInfo fieldInfo = member as FieldInfo;
- PropertyInfo propertyInfo = member as PropertyInfo;
- Type elementType;
- object elementValue;
- string elementName;
- string elementNs;
- if (fieldInfo != null) {
- elementType = fieldInfo.FieldType;
- elementValue = fieldInfo.GetValue (o);
- }
- else {
- elementType = propertyInfo.PropertyType;
- elementValue = propertyInfo.GetValue (o, null);
- }
- elementName = xmlElements.GetElementName (elementType, member.Name);
- elementNs = xmlElements.GetElementNamespace (elementType);
- WriteElement (writer, xmlElements, elementName, elementNs, elementType, elementValue);
- }
- }
- [MonoTODO ("Remove FIXMEs")]
- private void WriteElement (XmlWriter writer, XmlAttributes attrs, string name, string ns, Type type, Object value)
- {
- if (IsInbuiltType (type)) {
- string xmlValue = GetXmlValue (value);
- if (xmlValue != String.Empty && xmlValue != null)
- writer.WriteElementString (name, ns, xmlValue);
- }
- else if (attrs.XmlText != null && value != null) {
- if (type == typeof (object[])) {
- // FIXME
- }
- else if (type == typeof (string[])) {
- // FIXME
- }
- else if (type == typeof (XmlNode)) {
- ((XmlNode) value).WriteTo (writer);
- }
- else if (type == typeof (XmlNode[])) {
- XmlNode[] nodes = (XmlNode[]) value;
- foreach (XmlNode node in nodes)
- node.WriteTo (writer);
- }
- }
- else if (type.IsArray && value != null) {
- writer.WriteStartElement (name, ns);
- SerializeArray (writer, value);
- writer.WriteEndElement ();
- }
- else if (value is ICollection) {
- BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
- //Find a non indexer Count Property with return type of int
- PropertyInfo countInfo = type.GetProperty ("Count", flags, null, typeof (int), new Type[0], null);
- PropertyInfo itemInfo = type.GetProperty ("Item", flags, null, null, new Type[1] {typeof (int)}, null);
- int count = (int) countInfo.GetValue (value, null);
- if (count > 0)
- for (int i = 0; i < count; i++) {
- object itemValue = itemInfo.GetValue (value, new object[1] {i});
- if (itemValue != null) {
- string itemName = attrs.GetElementName (itemValue.GetType (), name);
- string itemNs = attrs.GetElementNamespace (itemValue.GetType ());
- writer.WriteStartElement (itemName, itemNs);
- SerializeMembers (writer, itemValue, false);
- writer.WriteEndElement ();
- }
- }
- }
- else if (value is IEnumerable) {
- // FIXME
- }
- else if (type.IsEnum) {
- // FIXME
- }
- else if (value != null) { //Complex Type?
- string itemName = attrs.GetElementName (value.GetType (), name);
- string itemNs = attrs.GetElementNamespace (value.GetType ());
- writer.WriteStartElement (itemName, itemNs);
- SerializeMembers (writer, value, false);
- writer.WriteEndElement ();
- }
- else {
- // FIXME
- }
- }
- [MonoTODO]
- private void SerializeArray (XmlWriter writer, object o)
- {
- throw new NotImplementedException ();
- }
- /// <summary>
- /// If the type is a string, valuetype or primitive type we do not populate the TypeTable.
- /// If the type is an array, we populate the TypeTable with Element type of the array.
- /// If the type implements ICollection, it is handled differently. We do not care for its members.
- /// If the type implements IEnumberable, we check that it implements Add(). Don't care for members.
- /// </summary>
- [MonoTODO ("Remove FIXMEs")]
- private void FillTypeTable (Type type)
- {
- if (typeTable.Contains (type))
- return;
- //For value types and strings we don't need the members.
- //FIXME: We will need the enum types probably.
- if (IsInbuiltType (type))
- return;
- //Array, ICollection and IEnumberable are treated differenty
- if (type.IsArray) {
- FillArrayType (type);
- return;
- }
- else if (type.IsEnum) {
- FillEnum (type);
- return;
- }
- else {
- //There must be a public constructor
- if (!HasDefaultConstructor (type))
- throw new Exception ("Can't Serialize Type " + type.Name + " since it does not have default Constructor");
- if (type.GetInterface ("ICollection") == typeof (System.Collections.ICollection)) {
- FillICollectionType (type);
- return;
- }
- if (type.GetInterface ("IEnumerable") == typeof (System.Collections.IEnumerable)) {
- //FillIEnumerableType(type);
- //return;
- }
- }
-
- //Add the Class to the hashtable.
- //Each value of the hashtable has two objects, one is the hashtable with key of membername (for deserialization)
- //Other is an Array of XmlSerializernames, Array of XmlAttributes & Array of XmlElements.
- Object[] memberObj = new Object[4];
- typeTable.Add (type,memberObj);
- Hashtable memberTable = new Hashtable ();
- memberObj[0] = memberTable;
- memberTable.Add ("", XmlAttributes.FromClass (type));
- memberObj[1] = null;
- ArrayList attributes = new ArrayList ();
- memberObj[2] = attributes;
- ArrayList elements = new ArrayList ();
- memberObj[3] = elements;
- //Get the graph of the members. Graph is nothing but the order
- //in which MS implementation serializes the members.
- MemberInfo[] minfo = GetGraph (type);
- foreach (MemberInfo member in minfo) {
- FieldInfo fieldInfo = (member as FieldInfo);
- PropertyInfo propertyInfo = (member as PropertyInfo);
- if (fieldInfo != null) {
- //If field is readOnly or const, do not serialize it.
- if (fieldInfo.IsLiteral || fieldInfo.IsInitOnly)
- continue;
- XmlAttributes xmlAttributes = XmlAttributes.FromField (member, fieldInfo);
- //If XmlAttributes have XmlIgnore, ignore this member
- if (xmlAttributes.XmlIgnore)
- continue;
- //If this member is a XmlNs type, set the XmlNs object.
- if (xmlAttributes.Xmlns) {
- memberObj[1] = xmlAttributes;
- continue;
- }
- //If the member is a attribute Type, Add to attribute list
- if (xmlAttributes.isAttribute)
- attributes.Add (xmlAttributes);
- else //Add to elements
- elements.Add (xmlAttributes);
- //Add in the Hashtable.
- memberTable.Add (member.Name, xmlAttributes);
-
- if (xmlAttributes.XmlAnyAttribute != null || xmlAttributes.XmlText != null)
- continue;
- if (xmlAttributes.XmlElements.Count > 0) {
- foreach (XmlElementAttribute elem in xmlAttributes.XmlElements) {
- if (elem.Type != null)
- FillTypeTable (elem.Type);
- else
- FillTypeTable (fieldInfo.FieldType);
- }
- continue;
- }
- if (!IsInbuiltType (fieldInfo.FieldType))
- FillTypeTable (fieldInfo.FieldType);
- }
- else if (propertyInfo != null) {
- //If property is readonly or writeonly, do not serialize it.
- //Exceptions are properties whose return type is array, ICollection or IEnumerable
- //Indexers are not serialized unless the class Implements ICollection.
- if (!(propertyInfo.PropertyType.IsArray || Implements (propertyInfo.PropertyType, typeof (ICollection)) ||
- (propertyInfo.PropertyType != typeof (string) && Implements (propertyInfo.PropertyType, typeof (IEnumerable))))) {
- if(!(propertyInfo.CanRead && propertyInfo.CanWrite) || propertyInfo.GetIndexParameters ().Length != 0)
- continue;
- }
- XmlAttributes xmlAttributes = XmlAttributes.FromProperty (member, propertyInfo);
- // If XmlAttributes have XmlIgnore, ignore this member
- if (xmlAttributes.XmlIgnore)
- continue;
- // If this member is a XmlNs type, set the XmlNs object.
- if (xmlAttributes.Xmlns) {
- memberObj[1] = xmlAttributes;
- continue;
- }
- // If the member is a attribute Type, Add to attribute list
- if (xmlAttributes.isAttribute)
- attributes.Add (xmlAttributes);
- else //Add to elements
- elements.Add (xmlAttributes);
- // OtherWise add in the Hashtable.
- memberTable.Add (member.Name, xmlAttributes);
- if (xmlAttributes.XmlAnyAttribute != null || xmlAttributes.XmlText != null)
- continue;
- if (xmlAttributes.XmlElements.Count > 0) {
- foreach (XmlElementAttribute elem in xmlAttributes.XmlElements) {
- if (elem.Type != null)
- FillTypeTable (elem.Type);
- else
- FillTypeTable (propertyInfo.PropertyType);
- }
- continue;
- }
- if (!IsInbuiltType (propertyInfo.PropertyType))
- FillTypeTable (propertyInfo.PropertyType);
- }
- }
- // Sort the attributes for the members according to their Order
- // This is an extension to MS's Implementation and will be useful
- // if our reflection does not return the same order of elements
- // as MS .NET impl
- if (useOrder)
- BubbleSort (elements, XmlAttributes.attrComparer);
- }
- private void FillArrayType (Type type)
- {
- if (type.GetArrayRank () != 1)
- throw new Exception ("MultiDimensional Arrays are not Supported");
- Type arrayType = type.GetElementType ();
- if (arrayType.IsArray)
- FillArrayType (arrayType);
- else if (!IsInbuiltType (arrayType))
- FillTypeTable (arrayType);
- }
- private void FillICollectionType (Type type)
- {
- //Must have an public Indexer that takes an integer and
- //a public Count Property which returns an int.
- BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
-
- //Find a non indexer Count Property with return type of int
- PropertyInfo countProp = type.GetProperty ("Count", flags, null, typeof (int), new Type[0], null);
- if (countProp == null || !countProp.CanRead)
- throw new Exception ("Cannot Serialize " + type + " because it implements ICollectoion, but does not implement public Count property");
- //Find a indexer Item Property which takes an int
- PropertyInfo itemProp = type.GetProperty ("Item", flags, null, null, new Type[1] {typeof (int)}, null);
- if (itemProp == null || !itemProp.CanRead || !itemProp.CanWrite)
- throw new Exception ("Cannot Serialize " + type + " because it does not have a read/write indexer property that takes an int as argument");
- FillTypeTable (itemProp.PropertyType);
- }
- [MonoTODO]
- private void FillIEnumerableType (Type type)
- {
- //Must implement a public Add method that takes a single parameter.
- //The Add method's parameter must be of the same type as is returned from
- //the Current property on the value returned from GetEnumerator, or one of that type's bases.
- // We currently ignore enumerable types anyway, so this method was junked.
- // The code did not do what the documentation above says (if that is even possible!)
- return;
- }
- private void FillEnum (Type type)
- {
- Hashtable memberTable = new Hashtable ();
- BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
- typeTable.Add (type, memberTable);
- string[] names = Enum.GetNames (type);
- foreach (string name in names) {
- MemberInfo[] members = type.GetMember (name);
- if (members.Length != 1)
- throw new Exception("Should never happen. Enum member not present or more than one. " + name);
- XmlAttributes xmlAttributes = new XmlAttributes (members[0]);
- if (xmlAttributes.XmlIgnore)
- continue;
- if (xmlAttributes.XmlEnum != null)
- memberTable.Add (members[0].Name, xmlAttributes.XmlEnum.Name);
- else
- memberTable.Add (members[0].Name, members[0].Name);
- }
- }
- private bool HasDefaultConstructor (Type type)
- {
- ConstructorInfo defaultConstructor = type.GetConstructor (new Type[0]);
- if (defaultConstructor == null || defaultConstructor.IsAbstract || defaultConstructor.IsStatic || !defaultConstructor.IsPublic)
- return false;
-
- return true;
- }
- private bool IsInbuiltType (Type type)
- {
- if (type.IsEnum)
- return false;
- if (type.IsValueType || type == typeof (string) || type.IsPrimitive)
- return true;
- if (type == typeof (DateTime) || type == typeof (XmlNode))
- return true;
-
- return false;
- }
- private static MemberInfo[] GetGraph(Type type)
- {
- ArrayList typeGraph = new ArrayList ();
- GetGraph (type, typeGraph);
- return (MemberInfo[]) typeGraph.ToArray (typeof (MemberInfo));
- }
- private static void GetGraph (Type type, ArrayList typeGraph)
- {
- BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
- if (type.BaseType == null)
- return;
- GetGraph (type.BaseType, typeGraph);
- typeGraph.AddRange (type.GetFields (flags));
- typeGraph.AddRange (type.GetProperties (flags));
- }
- private string GetXmlValue (object value)
- {
- if (value == null)
- return null;
- if (value is Enum) {
- Type type = value.GetType ();
-
- if (typeTable.ContainsKey (type)) {
- Hashtable memberTable = (Hashtable) (typeTable[type]);
- if (type.IsDefined (typeof (FlagsAttribute), false)) {
- //If value is exactly a single enum member
- if (memberTable.Contains (value.ToString ()))
- return (string) memberTable[value.ToString ()];
- string retval = "";
- int enumval = (int) value;
- string[] names = Enum.GetNames (type);
- foreach (string key in names) {
- if (!memberTable.ContainsKey (key))
- continue;
- //Otherwise multiple values.
- int val = (int) Enum.Parse (type, key);
- if (val != 0 && (enumval & val) == val)
- retval += " " + (string) memberTable[Enum.GetName (type, val)];
- }
- retval = retval.Trim ();
- if (retval.Length == 0)
- return null;
- return retval;
- }
- else if (memberTable.ContainsKey (value.ToString ()))
- return (string) memberTable[value.ToString()];
- else
- return null;
- }
- else
- throw new Exception ("Unknown Enumeration");
- }
- if (value is bool)
- return (bool) value ? "true" : "false";
- if (value is XmlQualifiedName) {
- if (((XmlQualifiedName) value).IsEmpty)
- return null;
- }
- return (value == null) ? null : value.ToString ();
- }
- [MonoTODO ("Remove FIXMEs")]
- private static void ProcessAttributes (XmlAttributes attrs, Hashtable memberTable)
- {
- if (attrs.XmlAnyAttribute != null) {
- // FIXME
- }
- foreach (XmlAnyElementAttribute anyelem in attrs.XmlAnyElements)
- memberTable.Add (new XmlQualifiedName (anyelem.Name, anyelem.Namespace), attrs);
- if (attrs.XmlArray != null) {
- // FIXME
- }
- foreach (XmlArrayItemAttribute item in attrs.XmlArrayItems)
- memberTable.Add (new XmlQualifiedName (item.ElementName, item.Namespace), attrs);
- if (attrs.XmlAttribute != null)
- memberTable.Add (new XmlQualifiedName (attrs.XmlAttribute.AttributeName,attrs.XmlAttribute.Namespace), attrs);
- if (attrs.XmlChoiceIdentifier != null) {
- // FIXME
- }
- foreach (XmlElementAttribute elem in attrs.XmlElements)
- memberTable.Add (new XmlQualifiedName (elem.ElementName, elem.Namespace), attrs);
- if (attrs.XmlEnum != null) {
- // FIXME
- }
- if (attrs.XmlType != null)
- memberTable.Add (new XmlQualifiedName (attrs.XmlType.TypeName, attrs.XmlType.Namespace), attrs);
- }
- private bool Implements (Type type, Type interfaceType)
- {
- return (type.GetInterface (interfaceType.Name) == interfaceType);
- }
- private static void BubbleSort (ArrayList array, IComparer comparer)
- {
- int len = array.Count;
- object obj1, obj2;
- for (int i=0; i < len; i++) {
- for (int j=0; j < len -i -1; j++) {
- obj1 = array[j];
- obj2 = array[j+1];
- if (comparer.Compare (obj2 , obj1 ) < 0) {
- array[j] = obj2;
- array[j+1] = obj1;
- }
- }
- }
- }
- #endregion // Methods
- }
- }
|