简体   繁体   English

从BindingList派生的类的公共字段/属性 <T> 不会序列化

[英]Public fields/properties of a class derived from BindingList<T> wont serialize

I'm trying to serialize a class that derives from BindingList(Floor) , where Floor is a simple class that only contains a property Floor.Height 我正在尝试序列化一个从BindingList(Floor)派生的类,其中Floor是一个仅包含属性Floor.Height的简单类。

Here's a simplified version of my class 这是我班的简化版

[Serializable]
[XmlRoot(ElementName = "CustomBindingList")]
public class CustomBindingList:BindingList<Floor>
{
    [XmlAttribute("publicField")]
    public string publicField;
    private string privateField;

    [XmlAttribute("PublicProperty")]
    public string PublicProperty
    {
        get { return privateField; }
        set { privateField = value; }
    }
}

I'll serialize an instance of CustomBindingList using the following code. 我将使用以下代码序列化CustomBindingList的实例。

XmlSerializer ser = new XmlSerializer(typeof(CustomBindingList));
StringWriter sw = new StringWriter();

CustomBindingList cLIst = new CustomBindingList();

Floor fl;

fl = new Floor();
fl.Height = 10;
cLIst.Add(fl);

fl = new Floor();
fl.Height = 10;    
cLIst.Add(fl);

fl = new Floor();
fl.Height = 10;
cLIst.Add(fl);

ser.Serialize(sw, cLIst);

string testString = sw.ToString();

Yet testString above ends getting set to the following XML: 然而,上面的testString最终被设置为以下XML:

<CustomBindingList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
    <Floor Height="10" />
    <Floor Height="10" />
    <Floor Height="10" />
</CustomBindingList>"

How do I get "publicField" or "publicProperty to serialize as well? 如何获取“ publicField”或“ publicProperty”进行序列化?

The short answer here is that .NET generally expects something to be a collection xor to have properties. 简短的回答是,.NET通常期望某物成为具有属性的xor集合。 This manifests in a couple of places: 这体现在几个地方:

  • xml serialization; xml序列化; properties of collections aren't serialized 集合的属性未序列化
  • data-binding; 数据绑定; you can't data-bind to properties on collections, as it implicitly takes you to the first item instead 您无法将数据绑定到集合的属性,因为它隐式地将您带到第一项

In the case of xml serialization, it makes sense if you consider that it might just be a SomeType[] at the client... where would the extra data go? 在xml序列化的情况下,如果您认为它可能只是客户端上的SomeType[] ,那是有道理的...多余的数据会放在哪里?

The common solution is to encapsulate a collection - ie rather than 常见的解决方案是封装集合-即而不是

public class MyType : List<MyItemType> // or BindingList<...>
{
    public string Name {get;set;}
}

public class MyType
{
    public string Name {get;set;}
    public List<MyItemType> Items {get;set;} // or BindingList<...>
}

Normally I wouldn't have a set on a collection property, but XmlSerializer demands it... 通常我不会在collection属性上set一个集合,但是XmlSerializer要求它...

XML serialization handles collections in a specific way, and never serializes the fields or properties of the collection, only the items. XML序列化以特定方式处理集合,并且从不对集合的字段或属性(仅对项目)进行序列化。

You could either : 您可以:

  • implement IXmlSerializable to generate and parse the XML yourself (but it's a lot of work) 实现IXmlSerializable来自己生成和解析XML(但这是很多工作)
  • wrap your BindingList in another class, in which you declare your custom fields (as suggested by speps) 将BindingList包装在另一个类中,在其中声明您的自定义字段(如speps所建议)

This is known issue with XML serialization and inheriting from collections. 这是XML序列化和从集合继承的已知问题。

You can read more info on this here : http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/0d94c4f8-767a-4d0f-8c95-f4797cd0ab8e 您可以在此处阅读更多信息: http : //social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/0d94c4f8-767a-4d0f-8c95-f4797cd0ab8e

You could try something like this : 您可以尝试这样的事情:

[Serializable]
[XmlRoot]
public class CustomBindingList
{
    [XmlAttribute]
    public string publicField;
    private string privateField;

    [XmlAttribute]
    public string PublicProperty
    {
        get { return privateField; }
        set { privateField = value; }
    }

    [XmlElement]
    public BindingList<Floor> Floors = new BindingList<Floor>();
}

This means you can add floors by using Floors.Add and you will get the result you want, I hope, however, I didn't try it. 这意味着您可以使用Floors.Add添加楼层,您将获得想要的结果,但是,我没有尝试过。 Keep in mind that playing around with attributes is the key to XML serialization. 请记住,使用属性是XML序列化的关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM