Exemple #1
0
        public void ReadAttribute(SimpleAttributeAttribute attribute, object reader,
                                  object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            object value = ReadSimpleValue(reader, receiver, settings, info, attribute.AutoTrim);

            SerializerUtil.CheckRequiredAttribute(attribute, receiver, info,
                                                  value.ConvertToString());
        }
Exemple #2
0
 public static void CheckRequiredAttribute(SimpleAttributeAttribute attribute,
                                           Object receiver, ObjectPropertyInfo info, String value)
 {
     if (attribute.Required && string.IsNullOrEmpty(value))
     {
         TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                     "属性{0}标记了Required,但是对应的属性没有配置值", info.PropertyName), receiver);
     }
 }
Exemple #3
0
        internal ObjectInfo(object receiver, Type type, string modelName)
            : this()
        {
            IsObjectContext = Attribute.GetCustomAttribute(type,
                                                           typeof(ObjectContextAttribute), false) != null;

            // 扫描对象的每个属性,根据属性所标注的特性,配置Attribute,Content和Element子项
            PropertyInfo[] properties = type.GetProperties(BIND_ATTRIBUTE);

            foreach (var property in properties)
            {
                object[] propertyAttributes = property.GetCustomAttributes(false);
                if (propertyAttributes.Length == 0)
                {
                    continue;
                }

                var attribute = (from attr in propertyAttributes
                                 where attr is SimpleAttributeAttribute
                                 select attr).FirstOrDefault();
                if (attribute != null)
                {
                    SimpleAttributeAttribute simpleAttr = attribute as SimpleAttributeAttribute;
                    simpleAttr.SetNameMode(modelName, property);
                    ObjectPropertyInfo attrInfo = new ReflectorObjectPropertyInfo(
                        property, simpleAttr, modelName);
                    fAttributes.Add(attrInfo.QName.LocalName, attrInfo);
                    continue;
                }

                attribute = (from attr in propertyAttributes
                             where (attr is TextContentAttribute || attr is ComplexContentAttribute)
                             select attr).FirstOrDefault();
                if (attribute != null)
                {
                    AssertContent(receiver, type, fContent);
                    fContent = new ReflectorObjectPropertyInfo(property,
                                                               attribute as BaseObjectAttribute, modelName);
                    continue;
                }

                attribute = (from attr in propertyAttributes
                             where attr is TagElementAttribute
                             select attr).FirstOrDefault();
                if (attribute != null)
                {
                    TagElementAttribute tagAttr = attribute.Convert <TagElementAttribute>();
                    tagAttr.SetNameMode(modelName, property);
                    fElements.Add(type, property, tagAttr, modelName);
                    tagAttr.Read(type, property, modelName, propertyAttributes);
                    continue;
                }

                fElements.ReadElementAttribute(type, property, modelName, propertyAttributes);
            }
        }
Exemple #4
0
        public void ReadAttribute(SimpleAttributeAttribute attribute, object reader,
                                  object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            XName      name  = info.QName.ToXName();
            XAttribute attr  = current.Attribute(name);
            string     value = attr == null ? null : attr.Value;

            SerializerUtil.CheckRequiredAttribute(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, attribute.AutoTrim);
        }
Exemple #5
0
        public void WriteAttribute(SimpleAttributeAttribute attribute, object writer,
                                   object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            if (ObjectUtil.IsDefaultValue(attribute.DefaultValue, value))
            {
                return;
            }

            XName      name    = info.QName.ToXName();
            XAttribute attr    = new XAttribute(name, ObjectUtil.ToString(info.Converter, value, settings));
            XElement   current = serializerData.Convert <XElement>();

            current.Add(attr);
        }
        public void ReadAttribute(SimpleAttributeAttribute attribute, object reader,
                                  object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml = reader.Convert <XmlReader>();

            string localName = info.LocalName;
            string value     = null;

            switch (attribute.NamespaceType)
            {
            case NamespaceType.None:
                value = xml.GetAttribute(localName);
                break;

            case NamespaceType.Toolkit:
            case NamespaceType.Namespace:
                value = xml.GetAttribute(localName, attribute.NamespaceUri);
                break;
            }

            SerializerUtil.CheckRequiredAttribute(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, attribute.AutoTrim);
        }
        public void WriteAttribute(SimpleAttributeAttribute attribute, object writer,
                                   object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            if (ObjectUtil.IsDefaultValue(attribute.DefaultValue, value))
            {
                return;
            }

            XmlWriter xmlWriter = writer.Convert <XmlWriter>();
            QName     name      = info.QName;

            if (name.HasNamespace)
            {
                PrefixTable prefix = serializerData.Convert <PrefixTable>();
                xmlWriter.WriteAttributeString(prefix.GetPrefix(name.Namespace), name.LocalName,
                                               name.Namespace, ObjectUtil.ToString(info.Converter, value, settings));
            }
            else
            {
                xmlWriter.WriteAttributeString(name.LocalName,
                                               ObjectUtil.ToString(info.Converter, value, settings));
            }
        }
Exemple #8
0
 public void ReadAttribute(SimpleAttributeAttribute attribute, object reader,
                           object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     throw new NotSupportedException();
 }
Exemple #9
0
 public void WriteAttribute(SimpleAttributeAttribute attribute, object writer,
                            object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     throw new NotImplementedException();
 }
Exemple #10
0
 public void WriteAttribute(SimpleAttributeAttribute attribute, object writer,
                            object value, WriteSettings settings, ObjectPropertyInfo info,
                            object serializerData)
 {
     WriteSimpleElement(attribute, writer, value, settings, info, attribute.UseSourceType);
 }
 public void WriteAttribute(SimpleAttributeAttribute attribute, object writer,
                            object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     SetValue(writer, info, info.LocalName, value, settings);
 }
Exemple #12
0
 public void ReadAttribute(SimpleAttributeAttribute attribute, object reader, object receiver,
                           ReadSettings settings, ObjectPropertyInfo info, object serializerData)
 {
     SerializerUtil.SetObjectValue(receiver, settings, info,
                                   GetValue(reader, info.LocalName), attribute.AutoTrim);
 }