T>一个名单,其中财产;不反序列化 [英] Property of a List<T> Not Deserialized

查看:121
本文介绍了T>一个名单,其中财产;不反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

属性 FilePattern 没有得到从XML反序列化:

The property FilePattern does not get deserialized from the xml:

<Parser FilePattern='*'/>

[Serializable(), XmlRoot("Parser")]
public class Parser : List<System.DateTime>
{

    private string _FilePattern;
    [XmlAttribute()]
    public string FilePattern {
        get { return _FilePattern; }
        set { _FilePattern = value; }
    }

}
private void ParserTest()
{
    Parser Parser = Serialization.Deserialize("<Parser FilePattern='*'/>", typeof(Parser));
    // Here, Parser.FilePattern is null
}

解析器就是一个类。我怎样才能使它填充 FilePattern 属性?

Parser is just a class. How can I make it populate the FilePattern property?

推荐答案

有时候你想做什么,你只想做;该框架被定罪。

下面是,在我的情况为我工作的程序的第一稿​​。随着它的发展随着时间的推移,我会更新我的答案。

Here's a first draft of a routine that worked for me in my situation. As it evolves over time I will update my answer.

我在连载辅助模块,在这份旁边我的其他反序列化方法正常类。

I have located this in my Serialization helper module alongside my other Deserialize methods for "normal" classes.

基本上,它是一个正常的反序列化,让XML解串器所有繁重的任务,然后将其用XML元素的属性新近反序列化对象。

Basically, what it does is a normal deserialization, to let the xml deserializer to all the heavy lifting, then it populates the newly deserialized object with the properties of the xml element.

就像我说的,这是一个草案,但马克Gravell说,这是一个大量的工作,所以我不得不这样做(做岂不是大量的工作给你)!

Like I say, it's a draft, but Marc Gravell said it was a lot of work, so I just had to do it (to make it not a lot of work for you)!

/// <summary>
/// Overcome limitation of xml serializer 
/// that it can't deserialize properties of classes 
/// that inherit from IEnumerable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Xml"></param>
/// <returns></returns>
/// <remarks></remarks>
public T Deserialize<T>(string Xml) where T : IEnumerable
{
    T functionReturnValue = default(T);
    //let the xml serializer do the rest of the work
    functionReturnValue = Deserialize(Xml, typeof(T));

    //copy over the additional properties
    using (XmlReader XmlReader = XmlTextReader.Create(new StringReader(Xml), new XmlReaderSettings {ValidationType = ValidationType.None,XmlResolver = null})) {
        XmlReader.MoveToContent();
        for (int Index = 0; Index <= XmlReader.AttributeCount - 1; Index++) {
            XmlReader.MoveToAttribute(Index);
            typeof(T).GetProperty(XmlReader.LocalName).SetValue(Deserialize(), XmlReader.Value, null);
        }
    }
    return functionReturnValue;
}

public object Deserialize(string Xml, Type Type)
{
    object functionReturnValue = null;
    functionReturnValue = null;
    if (Xml == string.Empty) {
        return null;
    }
    _Serializer = new XmlSerializer(Type);
    StringReader StringReader = new StringReader(Xml);
    functionReturnValue = Serializer.Deserialize(StringReader);
    if (functionReturnValue is IDeserializationEvents) {
        ((IDeserializationEvents)functionReturnValue).DeserializationComplete();
    }
    return functionReturnValue;
}

这篇关于T&GT;一个名单,其中财产;不反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆