XmlSerializer(c#)报告存在一个反映类型的错误(type = List)

有人可以帮忙吗?

我有一种将对象转换为数据集的方法.在这种情况下,对象是集合类的集合(使用LIST)

这是代码,但是在XmlSerializer formatter = …行上出现以下错误并出错.

这是错误

出现错误,反映了类型’System.Collections.Generic.List`1 [MyNameSpace.Model.InformeVehiculo]’.

    public static DataSet TransformObjectToDataSet(System.Type type, object datos, string nombre)
    {
        XmlSerializer formatter = new XmlSerializer(type, "Coleccion" + nombre);
        MemoryStream buffer = new MemoryStream();
        formatter.Serialize(buffer, datos);
        buffer.Position = 0;
        DataSet dtsDatos = new DataSet(nombre);
        dtsDatos.ReadXml(buffer);

        return dtsDatos;
    }

我正在这样调用方法

   TransformObjectToDataSet(typeof(List<InformeVehiculo>), objColeccionInformeVehiculo, "ColeccionInformeVehiculo");

objColeccionInformeVehiculo是一个列表

我有点迷茫,有谁知道为什么它失败了

编辑

在进一步查看InnerExeption之后,最后一个显示以下内容

 {"Cannot serialize member MyNameSpace.Model.ObjectChangeTracker.ObjectsRemovedFromCollectionProperties of type MyNameSpace.Model.ObjectsRemovedFromCollectionProperties, because it implements IDictionary."}

实际上,在InformeVehiculo内部,我已对其进行扩展以保留对InformeContracto的引用(使用部分类,它是最初由实体框架创建的模型)

InformeContrato就是失败的地方.

这是我的课

 public partial class InformeContrato : IObjectWithChangeTracker, INotifyPropertyChanged

与InformeVehiculo不同,InformeContrato不存在,后者是由实体框架创建的部分类.因此,我手动创建了InformeContrato并插入了changeTracking内容.

再往下,我有一个从实体框架创建的MODEL复制的区域.

    #region ChangeTracking

    protected virtual void OnPropertyChanged(String propertyName)
    {
        if (ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
        {
            ChangeTracker.State = ObjectState.Modified;
        }
        if (_propertyChanged != null)
        {
            _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    protected virtual void OnNavigationPropertyChanged(String propertyName)
    {
        if (_propertyChanged != null)
        {
            _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged { add { _propertyChanged += value; } remove { _propertyChanged -= value; } }
    private event PropertyChangedEventHandler _propertyChanged;
    private ObjectChangeTracker _changeTracker;

    //[DataMember]
    public ObjectChangeTracker ChangeTracker
    {
        get
        {
            if (_changeTracker == null)
            {
                _changeTracker = new ObjectChangeTracker();
                _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
            }
            return _changeTracker;
        }
        set
        {
            if (_changeTracker != null)
            {
                _changeTracker.ObjectStateChanging -= HandleObjectStateChanging;
            }
            _changeTracker = value;
            if (_changeTracker != null)
            {
                _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
            }
        }
    }

    private void HandleObjectStateChanging(object sender, ObjectStateChangingEventArgs e)
    {
        if (e.NewState == ObjectState.Deleted)
        {
            ClearNavigationProperties();
        }
    }

    protected bool IsDeserializing { get; private set; }

    [OnDeserializing]
    public void OnDeserializingMethod(StreamingContext context)
    {
        IsDeserializing = true;
    }

    [OnDeserialized]
    public void OnDeserializedMethod(StreamingContext context)
    {
        IsDeserializing = false;
        ChangeTracker.ChangeTrackingEnabled = true;
    }

    protected virtual void ClearNavigationProperties()
    {
        //AccesorioContrato.Clear();
    }

    #endregion

解决方法:

IDictionary不可序列化.标准词典也没有.您将需要实现自定义词典类型以启用序列化.然后它将起作用.

EX用于XML序列化:

  [XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue>
    : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof (TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue));

        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
            return;

        while (reader.NodeType != XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");

            reader.ReadStartElement("key");
            TKey key = (TKey) keySerializer.Deserialize(reader);
            reader.ReadEndElement();

            reader.ReadStartElement("value");
            TValue value = (TValue) valueSerializer.Deserialize(reader);
            reader.ReadEndElement();

            Add(key, value);

            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof (TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue));

        foreach (TKey key in Keys)
        {
            writer.WriteStartElement("item");

            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();

            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
    }

    #endregion 
}
上一篇:document.documentElement.clientHeight 和 document.body.clientHeight


下一篇:CodeGo.net>如何序列化包含字典的类?