1

是否有任何简单的方法来查询异构集合,其中集合中的对象都派生自同一个基类,但有些可能是一种派生类型,有些可能是另一种?

例如,这是一个类层次结构:

public class Ship
{
    public string Name { get; set; }
    public string Description { get; set; }
}

public class SailingVessel : Ship
{
    public string Rig { get; set; }
    public int NumberOfMasts { get; set; }
}

public class MotorVessel : Ship
{
    public string Propulsion { get; set; }
    public decimal TopSpeed { get; set; }
}

这是我要查询的 XML 文档:

<?xml version="1.0" ?>
<ships xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ship xsi:type="sailingVessel">
    <name>Cutty Sark</name>
    <description>Tea clipper</description>
    <rig>Ship</rig>
    <numberOfMasts>3</numberOfMasts>
  </ship>
  <ship xsi:type="sailingVessel">
    <name>Peking</name>
    <description>Windjammer of the Flying P Line</description>
    <rig>Barque</rig>
    <numberOfMasts>4</numberOfMasts>
  </ship>
  <ship xsi:type="motorVessel">
    <name>HMS Hood</name>
    <description>Last British battlecruiser</description>
    <propulsion>SteamTurbine</propulsion>
    <topSpeed>28</topSpeed>
  </ship>
  <ship xsi:type="motorVessel">
    <name>RMS Queen Mary 2</name>
    <description>Last transatlantic passenger liner</description>
    <propulsion>IntegratedElectricPropulsion</propulsion>
    <topSpeed>30</topSpeed>
  </ship>
  <ship xsi:type="motorVessel">
    <name>USS Enterprise</name>
    <description>First nuclear-powered aircraft carrier</description>
    <propulsion>Nuclear</propulsion>
    <topSpeed>33.6</topSpeed>
  </ship>
</ships>

我可以查询 XML 文档并将其内容读入 Ship 对象列表:

        XDocument xmlDocument = XDocument.Load("Ships.xml")
        XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

        var records = (from record in xmlDocument.Descendants("ship")
                       let type = record.Attribute(xsi + "type").Value                         
                       select new Ship
                       {
                           Name = (string)record.Element("name"),
                           Description = (string)record.Element("description")
                       }).ToArray<Ship>();

这将返回以下内容:

Ship[0] (type: Ship):
    Name: Cutty Sark
    Description: Tea clipper
Ship[1] (type: Ship):
    Name: Peking
    Description: Windjammer of the Flying P Line
Ship[2] (type: Ship):
    Name: HMS Hood
    Description: Last British battlecruiser
Ship[3] (type: Ship):
    Name: RMS Queen Mary 2
    Description: Last transatlantic passenger liner
Ship[4] (type: Ship):
    Name: USS Enterprise
    Description: First nuclear-powered aircraft carrier

不过,我真正希望能够制作的是:

Ship[0] (type: SailingVessel):
    Name: Cutty Sark
    Description: Tea clipper
    Rig: Ship
    NumberOfMasts: 3
Ship[1] (type: SailingVessel):
    Name: Peking
    Description: Windjammer of the Flying P Line
    Rig: Barque
    NumberOfMasts: 4
Ship[2] (type: MotorVessel):
    Name: HMS Hood
    Description: Last British battlecruiser
    Propulsion: SteamTurbine
    TopSpeed: 28
Ship[3] (type: MotorVessel):
    Name: RMS Queen Mary 2
    Description: Last transatlantic passenger liner
    Propulsion: IntegratedElectricPropulsion
    TopSpeed: 30
Ship[4] (type: MotorVessel):
    Name: USS Enterprise
    Description: First nuclear-powered aircraft carrier
    Propulsion: Nuclear
    TopSpeed: 33.6

如何修改 LINQ 查询以根据需要初始化 SailingVessel 对象或 MotorVessel 对象,而不是基础 Ship 对象?

我是否必须进行两次选择,并在每个选择中复制基类属性(名称和描述)的对象初始化?这就是我能想到的,但我讨厌所涉及的代码重复。或者,是否有某种方法可以初始化基类的属性,并可以选择初始化 SailingVessel (Rig, NumberOfMasts) 或 MotorVessel (Propulsion, TopSpeed) 的其他属性?

4

1 回答 1

1

我个人会给每种类型一个静态FromXElement方法(或构造函数),然后创建一个Dictionary<string, Func<Ship>>这样的:

var factories = new Dictionary<string, Func<Ship>>
{
     { "sailingVessel", SailingVessel.FromXElement },
     { "motorVessl", MotorVessel.FromXElement },
     ...
};

那么您的查询将是:

var records = from record in xmlDocument.Descendants("ship")
              let type = record.Attribute(xsi + "type").Value 
              select factories[type](record);

你可以给Ship类一个受保护的构造函数XElement来提取公共属性,留下如下内容:

public class SailingVessel : Ship
{
    public Rig Rig { get; set; }
    public int NumberOfMasts { get; set; }

    private SailingVessel(XElement element) : base(element)
    {
        Rig = (Rig) Enum.Parse(typeof(Rig), (string) element.Element("Rig"));
        NumberOfMasts = (int) element.Element("NumberOfMasts");
    }

    // Don't really need this of course - could put constructor calls
    // into your factory instead. I like the flexibility of factory 
    // methods though, e.g. for caching etc.
    public static FromXElement(element)
    {
        return new SailingVessel(element);
    }
}

肯定会涉及到相当多的代码,但它们都相当简单,也很容易测试。

于 2012-08-04T06:44:27.707 回答