.Net - 将使用类“A”的列表复制到使用继承自“A”类并添加新项目\变量的类“B”的新列表中

时间:2013-01-23 18:29:39

标签: c# .net list class

我有一个创建如下的列表。它使用第三方包装器管理从电脑游戏下载xml信息: -

List<EveAI.Live.Asset> lst_eveai_characterabc_assets = eve_api.GetCharacterAssets();

资产的类定义是: -

namespace EveAI.Live
{
  [TypeConverter(typeof(ExpandableObjectConverter))]
  public class Asset
  {
    public Asset();

    public ContainerType Container { get; set; }
    public List<Asset> Contents { get; set; }
    public double ContentsVolume { get; }
    public bool IsSingleton { get; set; }
    public long ItemID { get; set; }
    [Obsolete("Will be removed in a future version, use LocationStation or      
    LocationSolarsystem or LocationConquerableStation instead")]
    public Station Location { get; set; }
    public ConquerableStation LocationConquerableStation { get; set; }
    public int LocationID { get; set; }
    public SolarSystem LocationSolarsystem { get; set; }
    public Station LocationStation { get; set; }
    public long Quantity { get; set; }
    public int RawQuantity { get; set; }
    public ProductType Type { get; set; }
    public int TypeID { get; set; }

    public override string ToString();
  }
}

我想将列表lst_eveai_characterabc_assets复制到一个使用类AssetUniverseIDs的新列表 - 它继承了类EveAI.Live.Asset类似于: -

public class AssetUniverseIDs : EveAI.Live.Asset
{
  public Int32 stationID {get;set;}
  public Int32 stationTypeID { get; set; }
  public Int32 corporationID { get; set; }
  public Int32 solarSystemID { get; set; }
  public string solarSystemName { get; set; }
  public double security { get; set; }
  public string securityClass { get; set; }
  public Int32 constellationID { get; set; }
  public Int32 regionID { get; set; }
  public string regionName { get; set; }
  public string stationName { get; set; }
}

但到目前为止,我无法将lst_eveai_characterabc_assets复制到使用继承类Assets的类AssetUniverseID的新列表。我怎么能做到这一点。

1 个答案:

答案 0 :(得分:1)

这里你最好的选择可能是复制构造函数:

public AssetUniverseIDs(EveAI.Live.Assett original)
{
     this.Container = original.Container;
     this.Contents = original.Contents;
     // ...
}

然后,您可以从资产列表中构建AssetUniverseID列表,如下所示:

List<AssetUniverseIDs> newList = 
    lst_eveai_characterabc_assets.Select(a => new AssetUniverseIDs(a)).ToList();

您是否希望AssetUniverseIDs继承Asset?也许只有一个Asset作为其属性之一的类就足够了吗?