【问题标题】:.Net - copy a list that uses class 'A' to a new list that uses class 'B' which inherits from class 'A' and adds new items\variables.Net - 将使用“A”类的列表复制到使用“B”类的新列表,该“B”类继承自“A”类并添加新项目\变量
【发布时间】:2013-01-23 18:29:38
【问题描述】:

我有一个如下创建的列表。它使用第三方包装器来管理从电脑游戏下载 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 的新列表中。请问我怎样才能做到这一点。

【问题讨论】:

  • 您介意通过删除不必要的成员来制作更小的样本并尝试更改名称以遵循默认的 C# 约定吗?
  • 这看起来像是在为游戏 Eve 构建一个机器人,我认为这违反了他们的服务条款。
  • 所以你需要继承 AssetUniverseIDs 的“lst_eveai_characterabc_assets”类的所有实例?
  • Re Malcolm - 管理 EVE 的公司 - CCP 提供了一个广泛的 API,允许程序员下载有关字符的 xml 信息。一般规则是,只要不对其他玩家造成不公平,就可以获取和管理角色数据。对于我正在做的事情,获取我的游戏角色资产数据是可以的。我正在使用 EveAI 包装器,它管理 xml 数据的较低级别下载及其缓存时间。

标签: c# .net list class


【解决方案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 作为其属性之一的类就足够了?

【讨论】:

  • List lst_eveai_characterabc_assets = eve_api.GetCharacterAssets();得到。很多我真的不需要。所以我可以 for 循环遍历所有 lst_eveai_characterabc_assets 并挑选我只需要使用类 AssetUniverseIDs 的列表,该类继承自 EveAI.Live.Asset 类,其中包含 lst_eveai_characterabc_assets 具有的一些但不是所有嵌套列表。
猜你喜欢
  • 1970-01-01
  • 2015-01-01
  • 2020-08-07
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
相关资源
最近更新 更多