开发者

XmlSerializer (c#) reports There was an error reflecting type (type = List<myclass> )

开发者 https://www.devze.com 2022-12-25 16:21 出处:网络
Can anyone help? I have a method for converting an object to a dataset. In this case the object is a collection (using LIST) of a a collection class

Can anyone help?

I have a method for converting an object to a dataset. In this case the object is a collection (using LIST) of a a collection class

Here is the code but it errors with the following error on the line XmlSerializer formatter= ...

This is the error

There was an error reflecting type '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;
    }

I am calling the method like so

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

objColeccionInformeVehiculo is a List

I am a little lost, does anyone know why its failing

EDIT

After looking further down the InnerExeption, the last one displays the following

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

Actually inside the InformeVehiculo i have extended it to hold a reference to InformeContracto (using partial classes, it is a model originally created by entity framework)

InformeContrato is where its failing.

This my class

 public partial class InformeContrato : IObjectWithChangeTracker, INotifyPropertyChanged

InformeContrato did NOT exist unlike InformeVehiculo which existed as a partial class created by entity framework. So I created InformeContrato manually and inserted the changeTracking stuff..

Further down I have a region which i copied from a MODEL created by Entity Framework.

    #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 is not serializable. Nor are the standard dictionaries. You will need to implement a custom dictionary type to enable serialization. Then it will work.

EX for XML Serialization:

  [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 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号