IST有办法序列化实现ICollection的类的公共属性 [英] Ist there a way to serialize public properties of a class that implements ICollection

查看:174
本文介绍了IST有办法序列化实现ICollection的类的公共属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现ICollection的为了方便类。当类被序列化到XML中,所有的公共属性被跳过,只有实际的集合被序列化。我读从MSDN

I have a class that implements ICollection for convenience. When the class gets serialized to XML all the public properties are skipped and only the actual collection gets serialized. I read the following from the MSDN

实现的ICollection或IEnumerable的类。

Classes that implement ICollection or IEnumerable.

      
  • 只有集合是序列化,而不是公共属性。
  •   

反正是有解决此问题?有没有办法有一个实现ICollection的类,仍然将输出的公共属性?还是我使用的XmlWriter和自己做?

Is there anyway to work around this? Is there a way to have a class that implements ICollection and still will output the public properties? Or do I have to use an XmlWriter and do it myself?

万一一个例子是必要的。

Just in case an example is needed.

public class Batch: ICollection<Foo>
{
    public string Bar { get; set; }

    public int Baz { get; set; }

    public List<Foo> Foos { get; private set; }

    public IEnumerator<Foo> GetEnumerator()
    {
        return Foos.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return Foos.GetEnumerator();
    }

    public void Add(Foo item)
    {
        Foos.Add(item);
    }

    public void Clear()
    {
        Foos.Clear();
    }

    public bool Contains(Foo item)
    {
        return Foos.Contains(item);
    }

    public void CopyTo(Foo[] array, int arrayIndex)
    {
        Foos.CopyTo(array, arrayIndex);
    }

    public bool Remove(Foo item)
    {
        return Foos.Remove(item);
    }

    public int Count { get { return Foos.Count; } }

    public bool IsReadOnly { get { return false; } }
}

原因是这种方式是,在我的code另一部分我处理事情的集合。的IList IList的等。但为批次有一个适用于每一个富但这些信息是相同的每个foo的一个特定的批次信息。也许就像一个批次ID或创建的日期时间。我希望能够治疗我的批处理和我的其他收藏品以同样的方式在我的code一些地方,因为我所关心的是一个集合,具有计数。我不关心,有些信息是相同的是收集每一个项目。

The reason it is this way is that in another part of my code I am dealing with collections of things. IList IList etc. But for the batch there is information that applies to every Foo but that information is the same for every foo for one particular batch. Like perhaps a batch ID or Created DateTime. I wanted to be able to treat my batch and my other collections in the same way in some parts of my code because all i care about that is a collection and has a count. I don't care that some information is identical to every item in that collection.

我想这样做的原因是进行序列化。我试图删除冗余信息。如果有另一种方式来构建我的课,我不失去很重要的次数那么我所有的耳朵。我希望,以避免产生只包含了这样一个简单的事情计数的另一个接口。这感觉脏,但它可能是正确的事情。

The reason I wanted to do that was for serialization. I was trying to remove redundant information. If there is another way to structure my class that I do not lose that very important count then I am all ears. I was hoping to avoid creating another interface that just contains a count for a simple thing like this. It felt dirty but it might be the right thing.

推荐答案

没有,基本上是这样。这是不是唯一的的XmlSerializer - 框架治疗的集合元素的是相互排斥的大部分地区。我的建议:不这样做,的。有什么事情,是的或者的集合的 XOR 的一个元素。

No, basically. This is not unique to XmlSerializer - large parts of the framework treat collections and elements as mutually exclusive. My suggestion: don't do that. Have something that is either a collection xor an element.

这并不意味着格式有改变 - 你通常可以的封装的名单(而不是的的列表),并使用 [的XmlElement (someName)] 来得到相同类型的输出,例如:

This does not mean the format has to change - you can usually encapsulate a list (rather than be a list) and use [XmlElement("someName")] to get the same type of output, for example:

public class PeopleWrapper
{
    [XmlAttribute("someValue")]
    public int SomeValue { get; set; }

    private readonly List<Person> people = new List<Person>();
    [XmlElement("person")]
    public List<Person> Items { get { return people; } }
}

那么,如果另一个类有:

then if another class had:

public PeopleWrapper People { get; set; }

你会得到,在XML:

you would get, in the xml:

<People someValue="123">
    <person>...</person>
    <person>...</person>
    <person>...</person>
</People>

这篇关于IST有办法序列化实现ICollection的类的公共属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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