有没有简单的方法来查询异构集合,其中集合中的对象都派生自相同的基类,但有些可能是一个派生类型,有些可能属于另一个?
例如,这是一个类层次结构:
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)的其他属性?
答案 0 :(得分: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);
}
}
肯定会涉及相当多的代码,但它们都相当简单,也很容易测试。