0

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

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

Asset 的类定义是:-

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 的类 AssetUniverseIDs 的新列表中。请问我怎么能做到这一点。

4

1 回答 1

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也许只拥有一个具有 a作为其属性之一的类就足够了?

于 2013-01-23T18:53:20.603 回答