XDocument 构造函数上的 ParamArray 问题

Problems with ParamArray on XDocument constructor

XDocument 有一个重载的构造函数。

XDocument()
XDocument([ParamArray] content: obj[])
XDocument(XDocument)
XDocument(XDeclaration,[ParamArray] content: obj[])

所以当我尝试创建一个 XDocument

    new XDocument(
        new XDeclaration("1.0", "utf-8", "no"),
        new XElement(XName.Get "Foo"))

它给我错误

A unique overload for method XDocument...

我假设无法确定是否使用

XDocument([ParamArray] content: obj[])

XDocument(XDeclaration,[ParamArray] content: obj[])

如何强制它选择正确的?

啊...我找到答案了

https://nbevans.wordpress.com/2015/04/15/super-skinny-xml-document-generation-with-f/

let XDeclaration version encoding standalone = XDeclaration(version, encoding, standalone)
let XLocalName localName namespaceName = XName.Get(localName, namespaceName)
let XName expandedName = XName.Get(expandedName)
let XDocument xdecl content = XDocument(xdecl, content |> Seq.map (fun v -> v :> obj) |> Seq.toArray)
let XComment (value:string) = XComment(value) :> obj
let XElementNS localName namespaceName content = XElement(XLocalName localName namespaceName, content |> Seq.map (fun v -> v :> obj) |> Seq.toArray) :> obj
let XElement expandedName content = XElement(XName expandedName, content |> Seq.map (fun v -> v :> obj) |> Seq.toArray) :> obj
let XAttributeNS localName namespaceName value = XAttribute(XLocalName localName namespaceName, value) :> obj
let XAttribute expandedName value = XAttribute(XName expandedName, value) :> obj

即看起来我必须明确地将我的内容转换为一个对象数组,这样它才能 select 正确的重载。

您可以:

  • 参数数组的框元素:box (new XElement(XName.Get "Foo"))
  • 显式显示数组,并将其拆箱到 obj []unbox [| new XElement(XName.Get "Foo") |]