当我尝试序列化此集合时,name属性未序列化.
public class BCollection<T> : List<T> where T : B_Button
{
public string Name { get; set; }
}
BCollection<BB_Button> bc = new BCollection<B_Button>();
bc.Name = "Name";// Not Serialized!
bc.Add(new BB_Button { ID = "id1", Text = "sometext" });
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(bc);
Run Code Online (Sandbox Code Playgroud)
只有当我创建一个新类(没有List<t>继承),并定义字符串Name属性和List<B_Button> bc = new List<B_Button>();属性时,我才能得到正确的结果.
在许多串行器(和数据绑定,其实),对象是任何一个实体或(异)的列表; 在列表中具有属性通常不受支持.我会重构封装列表:
public class Foo<T> {
public string Name {get;set;}
private readonly List<T> items = new List<T>();
public List<T> Items { get { return items; } }
}
Run Code Online (Sandbox Code Playgroud)
也; 您打算如何用JSON表示?IIRC JSON数组语法不允许额外的属性要么.