需要一个可通过接口公开并通过FxCop的可序列化列表

时间:2015-10-05 05:40:22

标签: c# serialization interface fxcop

我需要什么:我有一个类在其相当复杂的数据中有嵌套列表。我需要这个类来序列化。我需要通过接口公开这个列表而不复制列表(接口需要公开确切的列表,而不是它的转换副本),我需要传递FXCop。

我有一种方法可以通过旧版本的FX Cop完成此操作 - 我为我的列表创建了包装类。但是FXCop现在标记我的包装类并且不允许我做一个集合,因为它不希望我公开列表。另一方面,序列化没有发现它的列表并且没有集合就不会序列化。有没有人遇到这个,如果有这样的话找到了解决它的方法?

这是我正在尝试做的一个小例子:

Glide.with(this).load("imageURl").into(imageView);

这是我的测试驱动程序:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace MyExample
{
public class JustSomeData : MyExample.IJustSomeData
{
    public double Elbow
    {
        get;
        set;
    }

    public double Knees
    {
        get;
        set;
    }

    public double Toes
    {
        get;
        set;
    }   
}

public class DataCollection : List<IJustSomeData>, IXmlSerializable
{
    private List<IJustSomeData> stuff = new List<IJustSomeData>();

    public DataCollection() : base() { }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<JustSomeData>));
        // Use the Deserialize method to restore the object's state.
        this.Clear();
        reader.ReadStartElement();
        List<JustSomeData> data = (List<JustSomeData>)serializer.Deserialize(reader);
        this.AddRange(data.ConvertAll(i => i as IJustSomeData));
        reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<JustSomeData>));
        // Use the Deserialize method to restore the object's state.
        List<JustSomeData> writeMe = this.ConvertAll(i => i as JustSomeData);
        serializer.Serialize(writer, writeMe);
    }
}

/// <summary>
///  this guy would be exposed through COM/etc
/// </summary>
public class IncredibleDataObject : MyExample.IIncredibleDataObject
{
    DataCollection collection = new DataCollection();  /// I need to expose the same list to customer
                                                       /// without converting it.  Convert all passes back a 
                                                       /// different list!!

    /// <summary>
    /// this guy is used for serialization
    /// </summary>
    public DataCollection DataCollection
    {
        get { return this.collection; }
        set { this.collection = value; }  // <-- Fx cop doesn't like, says I can serialize the collection with 
    }                                     // just the get and to not expose the set.  Serialization thinks otherwise and ignores
                                          // without the get. In the older FXCop version I got away with this!

    /// <summary>
    /// this guy exposed to customer/etc via interface
    /// </summary>
    [XmlIgnore]
    public IList<IJustSomeData> IDataCollection
    {
        get { return this.collection; }
    }
}
}

0 个答案:

没有答案