在F#中,我使用了XSD类型提供程序
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<section>
<div class="fullcontent"> 1
<div class="fullcontent--prev">Prev</div>
<div class="fullcontent--next">Next</div>
</div>
<div class="fullcontent"> 2
<div class="fullcontent--prev">Prev</div>
<div class="fullcontent--next">Next</div>
</div>
<div class="fullcontent"> 3
<div class="fullcontent--prev">Prev</div>
<div class="fullcontent--next">Next</div>
</div>
<div class="fullcontent"> 4
<div class="fullcontent--prev">Prev</div>
<div class="fullcontent--next">Next</div>
</div>
</section>
我可以实例化:
type nfe = XmlProvider<Schema="nfe_v4.00.xsd", ResolutionFolder=xsdFolder (*"c:/temp/nfe"*)>
但我无法创建另一个实例,指定一个构造函数属性:
let i = Unchecked.defaultof<nfe.InfNFe>
let defSign = Unchecked.defaultof<nfe.Signature>
nfe.NFe(infNFe=i, infNFeSupl=None, signature=defSign)
在该代码中,如果我尝试:
let ChangeDest (n: nfe.NFe) =
let novodest = nfe.Dest(cnpj=None,
cpf=Some "81679580515",
idEstrangeiro=None,
xNome=Some "TONY",
enderDest=None,
indIeDest="",
ie=None,
isuf=None,
im=None,
email=Some "tonyvca@hotmail.com"
)
let i = nfe.InfNFe(
versao="4.00",
id="NFe123 F#",
ide=n.InfNFe.Ide,
emit=n.InfNFe.Emit,
avulsa=None,
dest=Some novodest,
retirada=None,
entrega=None,
autXmLs=Unchecked.defaultof<nfe.AutXml[]>,
dets=Unchecked.defaultof<nfe.Det[]>,
total=Unchecked.defaultof<nfe.Total>,
transp=Unchecked.defaultof<nfe.Transp>,
cobr=None,
pag=Unchecked.defaultof<nfe.Pag>,
infAdic=None,
exporta=None,
compra=None,
cana=None
)
//let x = { n with InfNFe = i } // error FS0039: The record label 'InfNFe' is not defined
//let x ({ infNFe = i } : nfe.NFe) = n // error FS0039: The record label 'InfNFe' is not defined
let x = nfe.NFe(i, n.InfNFeSupl, n.Signature)
x
我得到了
错误FS0039:未定义记录标签“InfNFe”
我只能用这个编译:
let x = { n with InfNFe = i }
这是类型:
有没有办法在F#上复制构造函数?
这对于具有许多构造函数的类型非常有用,例如nfe.InfNFe在构造函数中有18个参数
答案 0 :(得分:1)
这就是我在评论中的意思(让可变性通过要更改的字段的XElement属性隐藏) &GT;
type nfe = XmlProvider<Schema="nfe_v4.00.xsd", ResolutionFolder="c:/temp/nfe">
let enderDest1 = nfe.EnderDest("test1", "", None, "", "", "", "", None, None, None, None)
let enderDest2 = nfe.EnderDest("test2", "", None, "", "", "", "", None, None, None, None)
let dest =
nfe.Dest(cnpj=None,
cpf=Some "81679580515",
idEstrangeiro=None,
xNome=Some "TONY",
enderDest=Some enderDest1,
indIeDest="",
ie=None,
isuf=None,
im=None,
email=Some "tonyvca@hotmail.com")
let replace (dest: nfe.Dest) (enderDest: nfe.EnderDest) =
match dest.EnderDest with
| Some x -> x.XElement.ReplaceWith(enderDest.XElement)
| None -> dest.XElement.Add(enderDest.XElement)
let xml1 = dest.XElement.ToString()
replace dest enderDest2
let xml2 = dest.XElement.ToString()