从收到的对象创建列表

本文关键字:创建 列表 对象 | 更新日期: 2023-09-27 18:35:57

我需要从我的类接收的对象创建一个列表。但我做不到吗?

[XmlTypeAttribute(AnonymousType = true)]
public class PackIt
{
    [XmlElement("pack")]
    public List<object> objects { get; set; }
    public PackIt(object model)
    {
         objects = new List<model.GetType()>();
    }
}

从收到的对象创建列表

你可以

PackIt泛型:

[XmlTypeAttribute(AnonymousType = true)]
public class PackIt<T>
{
    [XmlElement("pack")]
    public List<T> objects { get; set; }
    public PackIt()
    {
         objects = new List<T>();
    }
}
PachIt<string> packIt = new PackIt<string>();