【问题标题】:Create List from received object? [duplicate]从收到的对象创建列表? [复制]
【发布时间】:2013-10-17 20:37:44
【问题描述】:

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

[XmlTypeAttribute(AnonymousType = true)]
public class PackIt
{

    [XmlElement("pack")]
    public List<object> objects { get; set; }

    public PackIt(object model)
    {
         objects = new List<model.GetType()>();
    }

}

【问题讨论】:

    标签: c#


    【解决方案1】:

    您可以将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>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-07
      • 2017-06-26
      • 1970-01-01
      • 2018-01-02
      • 2018-02-23
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多