Exemple #1
0
        public ClassContentReader(
            IContentReaderCollection contentReaderCollection,
            IXmlAttributeInterpreter xmlAttributeInterpreter,
            OnDeserializeConfiguration onDeserializeConfiguration)
        {
            this.onDeserializeConfiguration = onDeserializeConfiguration;
            emitConstruction = ReadHelpers.EmitConstruction <T>();
            var infos =
                typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty);

            foreach (var propertyInfo in infos)
            {
                if (PropertyIsBad(propertyInfo))
                {
                    continue;
                }
                if (propertyInfo.IsDefined(typeof(XmlAttributeAttribute), false))
                {
                    var name = xmlAttributeInterpreter.GetXmlNodeName(propertyInfo);
                    attributeMap.Add(name, ReadHelpers.BuildSetter <T>(propertyInfo, contentReaderCollection));
                }
                else
                {
                    var name = xmlAttributeInterpreter.GetXmlNodeName(propertyInfo);
                    propertiesMap.Add(name, ReadHelpers.BuildSetter <T>(propertyInfo, contentReaderCollection));
                }
            }
        }
 public static ReportReader CreateReader(OnDeserializeConfiguration configuration = null)
 {
     if (configuration == null)
     {
         configuration = StandardConfigurations.EmptyOnDeserializeConfiguration;
     }
     return(new ReportReader(new ContentReaderCollection(new XmlAttributeInterpreter(), configuration)));
 }
        public StrictXmlSerializer()
        {
            var xmlAttributeInterpreter = new XmlAttributeInterpreter();

            var onDeserializeConfiguration = new OnDeserializeConfiguration();

            onDeserializeConfiguration.OnUnexpectedElement   += OnUnexpectedElement;
            onDeserializeConfiguration.OnUnexpectedAttribute += OnUnexpectedAttribute;

            reportReader = new ReportReader(new ContentReaderCollection(xmlAttributeInterpreter, onDeserializeConfiguration));
            reportWriter = new ReportWriter(new ContentWriterCollection(xmlAttributeInterpreter));
        }
        public void TestDuplicateItemsInArrayWithExceptionWhenDuplicateElement()
        {
            var configuration = new OnDeserializeConfiguration();

            configuration.OnDuplicateElement += delegate(object sender, DeserializationContext context) { throw new InvalidOperationException($"Duplicate element '{context.CurrentElementLocalName}'"); };

            Assert.That(() => ReportReaderHelpers.CreateReader(configuration).ReadFromString <CForDuplicateTest>(
                            @"<root>
    <B>x</B>
    <B>y</B>
    <A>2</A>
    <B>z</B>
</root>
"), Throws.Exception.TypeOf <InvalidOperationException>().With.Message.EqualTo("Duplicate element 'B'"));
        }
Exemple #5
0
        public ContentReaderCollection(IXmlAttributeInterpreter xmlAttributeInterpreter, OnDeserializeConfiguration onDeserializeConfiguration)
        {
            var leafContentReaders = new Dictionary <object, object>
            {
                { typeof(string), new StringContentReader() },
                { typeof(int), new SimpleContentReader <int>(int.TryParse) },
                { typeof(byte), new SimpleContentReader <byte>(byte.TryParse) },
                { typeof(long), new SimpleContentReader <long>(long.TryParse) },
                { typeof(bool), new SimpleContentReader <bool>(bool.TryParse) },
                { typeof(double), new FractionalContentReader <double>(double.TryParse) },
                { typeof(float), new FractionalContentReader <float>(float.TryParse) },
                { typeof(Guid), new SimpleContentReader <Guid>(Guid.TryParse) },
                { typeof(DateTime), new DateTimeContentReader() },
                { typeof(decimal), new FractionalContentReader <decimal>(decimal.TryParse) },
            };

            this.xmlAttributeInterpreter    = xmlAttributeInterpreter;
            this.onDeserializeConfiguration = onDeserializeConfiguration;
            foreach (var leafContentReader in leafContentReaders)
            {
                readers.Add(leafContentReader.Key, leafContentReader.Value);
            }
        }
 public XmlSerializerConfiguration()
 {
     OnDeserialize = new OnDeserializeConfiguration();
 }