XDocument 构造函数上的 ParamArray 问题



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"))

它给了我错误

方法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

即看起来我必须将我的内容显式转换为对象数组,以便它可以选择正确的重载。

您可以:

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

相关内容

  • 没有找到相关文章

最新更新