我試圖序列,從的BindingList(樓),其中樓是一個簡單的類僅包含一個屬性樓派生的類。身高公共字段/從的BindingList <T>派生類的屬性不會連載
這是我班的簡化版
[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; }
}
}
我會用下面的代碼序列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();
然而的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>"
如何獲得 「publicField」 或「公物序列化以及
從幾乎任何系統集合類派生是*非常糟糕的主意*。實現一個不擴展的接口。 – ShuggyCoUk 2009-08-04 13:25:50
我不想從IBindingList派生,因爲它不是泛型的,而且我不喜歡非泛型集合,因爲每次我想要做某些事情時,必須將其轉換爲真實的對象類型。 – 2009-08-04 16:03:15