protected override void WriteValue(XmlWriter writer, object value)
        {
            IDictionary dictionary = (IDictionary)value;

            foreach (object key in dictionary.Keys)
            {
                object target = dictionary[key];

                // any way to refactor code block?
                ReflectorTypeAttribute typeAttribute = ReflectorTypeAttribute.GetAttribute(target);
                if (typeAttribute == null)
                {
                    writer.WriteStartElement(elementName);
                    writer.WriteAttributeString(attribute.Key, key.ToString());
                    writer.WriteString(target.ToString());
                    writer.WriteEndElement();
                }
                else
                {
                    writer.WriteStartElement(typeAttribute.Name);
                    writer.WriteAttributeString(attribute.Key, key.ToString());

                    XmlTypeSerialiser serialiser = (XmlTypeSerialiser)typeAttribute.CreateSerialiser(target.GetType());
                    serialiser.WriteMembers(writer, target);

                    writer.WriteEndElement();
                }
            }
        }
        public void Write(object target)
        {
            NetReflector.CheckNull(target, "target", typeof(object));

            ReflectorTypeAttribute attribute = ReflectorTypeAttribute.GetAttribute(target);

            if (attribute == null)
            {
                throw new NetReflectorException(string.Format("Cannot serialise the object.  Target object type ({0}) is not marked with a ReflectorType attribute.", target.GetType()));
            }
            attribute.Write(writer, target);
        }
        protected virtual void WriteValue(XmlWriter writer, object value)
        {
            ReflectorTypeAttribute attribute = ReflectorTypeAttribute.GetAttribute(value);

            if (attribute == null)
            {
                writer.WriteString(value.ToString());
            }
            else
            {
                XmlTypeSerialiser serialiser = (XmlTypeSerialiser)attribute.CreateSerialiser(value.GetType());
                serialiser.WriteMembers(writer, value);
            }
        }
        public virtual void Write(XmlWriter writer, object target)
        {
            object value = member.GetValue(target);

            if (value != null && IsSerializableValue(value))
            {
                writer.WriteStartElement(attribute.Name);
                if (attribute.InstanceTypeKey != null)
                {
                    ReflectorTypeAttribute typeAttribute = ReflectorTypeAttribute.GetAttribute(value);
                    writer.WriteAttributeString(attribute.InstanceTypeKey, typeAttribute.Name);
                }
                WriteValue(writer, value);
                writer.WriteEndElement();
            }
        }
 protected virtual object Read(XmlNode childNode, Type instanceType, NetReflectorTypeTable table)
 {
     if (ReflectionUtil.IsCommonType(instanceType))
     {
         if ((childNode.Attributes != null) && (childNode.Attributes.Count > 0))
         {
             throw new NetReflectorException(
                       string.Format("Attributes are not allowed on {3} types - {0} attributes(s) found on '{1}'" + Environment.NewLine +
                                     "Xml: {2}",
                                     childNode.Attributes.Count,
                                     childNode.Name,
                                     childNode.OuterXml,
                                     instanceType.Name));
         }
         return(childNode.InnerText);
     }
     else
     {
         ReflectorTypeAttribute reflectorTypeAttribute = ReflectorTypeAttribute.GetAttribute(instanceType);
         if (reflectorTypeAttribute == null)
         {
             if (!string.IsNullOrEmpty(attribute.InstanceTypeKey))
             {
                 throw new NetReflectorException(
                           string.Format("Unable to find reflector type for '{0}' when deserialising '{1}' - '{3}' has not been set" + Environment.NewLine +
                                         "Xml: {2}",
                                         instanceType.Name,
                                         childNode.Name,
                                         childNode.OuterXml,
                                         attribute.InstanceTypeKey));
             }
             else
             {
                 throw new NetReflectorException(
                           string.Format("Unable to find reflector type for '{0}' when deserialising '{1}'" + Environment.NewLine +
                                         "Xml: {2}",
                                         instanceType.Name,
                                         childNode.Name,
                                         childNode.OuterXml));
             }
         }
         IXmlSerialiser serialiser = table[reflectorTypeAttribute.Name];
         // null check
         return(serialiser.Read(childNode, table));
     }
 }
示例#6
0
        public void Add(Type type)
        {
            ReflectorTypeAttribute attribute = ReflectorTypeAttribute.GetAttribute(type);

            if (attribute == null)
            {
                return;
            }

            if (!reflectorTypes.Contains(attribute.Name))
            {
                IXmlSerialiser serialiser = attribute.CreateSerialiser(type, instantiator);
                reflectorTypes.Add(attribute.Name, serialiser);
            }
            else if (type != this[attribute.Name].Type)
            {
                throw new NetReflectorException(string.Format(@"Multiple types exist with the same ReflectorTypeAttribute name ""{0}"": ({1}, {2})",
                                                              attribute.Name, type, this[attribute.Name].Type));
            }
        }
示例#7
0
        protected override void WriteValue(XmlWriter writer, object value)
        {
            foreach (object element in ((IEnumerable)value))
            {
                if (element == null)
                {
                    continue;
                }

                ReflectorTypeAttribute attribute = ReflectorTypeAttribute.GetAttribute(element);
                if (attribute == null)
                {
                    writer.WriteElementString(elementName, element.ToString());
                }
                else
                {
                    // make more concise?
                    IXmlSerialiser serialiser = attribute.CreateSerialiser(element.GetType());
                    serialiser.Write(writer, element);
                }
            }
        }
示例#8
0
 public XmlTypeSerialiser(Type type, ReflectorTypeAttribute attribute, IInstantiator instantiator)
 {
     this.type         = type;
     this.attribute    = attribute;
     this.instantiator = instantiator;
 }
示例#9
0
 public XmlTypeSerialiser(Type type, ReflectorTypeAttribute attribute) : this(type, attribute, new DefaultInstantiator())
 {
 }
 private bool IsSerializableValue(object value)
 {
     return(attribute.InstanceTypeKey == null || ReflectorTypeAttribute.GetAttribute(value) != null);
 }
示例#11
0
        private static void WriteXsdFile(string xsdFile, ReflectorTypeAttribute attribute, Type type, XDocument documentation, XElement typeElement)
        {
            using (var output = new StreamWriter(xsdFile))
            {
                WriteToOutput("Generating " + attribute.Name + ".xsd", OutputType.Info);
                output.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                output.WriteLine("<xs:schema targetNamespace=\"http://thoughtworks.org/ccnet/1/5\" elementFormDefault=\"qualified\" xmlns=\"http://thoughtworks.org/ccnet/1/5\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">");
                output.WriteLine("<xs:element name=\"" + attribute.Name + "\" type=\"" + attribute.Name + "\">");
                if (typeElement != null)
                {
                    var summary = RetrieveXmlData(typeElement, "summary");
                    if (summary != null)
                    {
                        output.WriteLine("<xs:annotation>");
                        output.WriteLine("<xs:documentation>");
                        output.WriteLine(MakeXmlSafe(summary));
                        output.WriteLine("</xs:documentation>");
                        output.WriteLine("</xs:annotation>");
                    }
                }

                output.WriteLine("</xs:element>");
                output.WriteLine("<xs:complexType name=\"" + attribute.Name + "\">");

                // Put all the elements into a dictionary since they can be either fields or properties :-(
                var elements = new Dictionary<MemberInfo, ReflectorPropertyAttribute>();
                foreach (var field in type.GetFields().OfType<MemberInfo>().Union(type.GetProperties()))
                {
                    var attributes = field.GetCustomAttributes(typeof(ReflectorPropertyAttribute), true);
                    if (attributes.Length > 0)
                    {
                        elements.Add(field, attributes[0] as ReflectorPropertyAttribute);
                    }
                }

                var enums = new List<Type>();
                if (elements.Count > 0)
                {
                    output.WriteLine("<xs:all>");
                    foreach (var element in elements)
                    {
                        var dataType = (element.Key is FieldInfo) ?
                            (element.Key as FieldInfo).FieldType :
                            (element.Key as PropertyInfo).PropertyType;
                        output.WriteLine(
                            "<xs:element name=\"" + element.Value.Name +
                            "\" type=\"" + dataType.Name +
                            "\" minOccurs=\"" + (element.Value.Required ? "1" : "0") +
                            "\" maxOccurs=\"1\">");

                        var memberName = (element.Key is FieldInfo ? "F:" : "P:") + element.Key.DeclaringType.FullName + "." + element.Key.Name;
                        var memberElement = (from xmlElement in documentation.Descendants("member")
                                             where xmlElement.Attribute("name").Value == memberName
                                             select xmlElement).SingleOrDefault();

                        if (memberElement != null)
                        {
                            var description = RetrieveXmlData(memberElement, "summary");
                            if (description != null)
                            {
                                output.WriteLine("<xs:annotation>");
                                output.WriteLine("<xs:documentation>");
                                output.WriteLine(MakeXmlSafe(description));
                                output.WriteLine("</xs:documentation>");
                                output.WriteLine("</xs:annotation>");
                            }
                        }
                        output.WriteLine("</xs:element>");
                        if (dataType.IsEnum)
                        {
                            enums.Add(dataType);
                        }
                    }

                    output.WriteLine("</xs:all>");
                }

                output.WriteLine("</xs:complexType>");

                foreach (var enumType in enums)
                {
                    output.WriteLine("<xs:simpleType name=\"" + enumType.Name + "\">");
                    output.WriteLine("<xs:restriction base=\"xs:string\">");
                    foreach (var value in Enum.GetNames(enumType))
                    {
                        output.WriteLine("<xs:enumeration value=\"" + value + "\"/>");
                    }

                    output.WriteLine("</xs:restriction>");
                    output.WriteLine("</xs:simpleType>");
                }

                output.WriteLine("</xs:schema>");
                output.Flush();
            }
        }
		public XmlTypeSerialiser(Type type, ReflectorTypeAttribute attribute, IInstantiator instantiator)
		{
			this.type = type;
			this.attribute = attribute;
			this.instantiator = instantiator;
		}
		public XmlTypeSerialiser(Type type, ReflectorTypeAttribute attribute) : this(type, attribute, new DefaultInstantiator())
		{}