Getting compile error on provided type

我怕爱的太早我们不能终老 提交于 2019-12-10 13:48:39

问题


I'm working on a TypeProvider that reads an XSD file and provides a type for each type defined in the XSD. However I have a problem in the below code

type schema = XmlProviders.Schema<"file.xsd">
type Bazzer = {
    Sum : XmlProviders.bar
}

on the last line I get a compilation error saying that XmlProviders.bar does not exist. The implementation of how I define the types are as follows

let defineType (xType : XElement) =
    let name = xType.Attribute(XName.Get "name").Value
    let t = ProvidedTypeDefinition(thisAssembly,
                                       ns,
                                       name,
                                       baseType = Some typeof<obj>)

    let ctor = ProvidedConstructor(parameters = [ ], 
                                   InvokeCode= (fun args -> <@@ "" :> obj @@>))
    t.AddMember ctor



 do provider.DefineStaticParameters(parameters, fun tyName args ->

    let filename = args.[0] :?> string
    let main = ProvidedTypeDefinition(thisAssembly,ns,
                                       tyName,
                                       baseType = Some typeof<obj>)

    //Elements is a list of XElement
    elements |> List.map defineType |> ignore
    main

I know that a XmlProviders.bar type is created because if I add an additional line to defineType provider.AddMember t then I get an error saying

The type provider 'XmlProviders.SampleTypeProvider' reported an error: container type for 'XmlProviders.bar' was already set to 'XmlProviders.Schema'

Where XmlProviders.Schema is the ProvidedTypeDefinition identified by provider

I'm a bit lost on why the compiler complains that the type is not there while if I explicitly add it I get the error that it's already there


回答1:


Found the answer, so to those that end in the same situation

the line

let t = ProvidedTypeDefinition(thisAssembly,
                                   ns,
                                   name,
                                   baseType = Some typeof<obj>)

where the nested type is defined should be without assembly and namespace

let t = ProvidedTypeDefinition(name,baseType = Some typeof<obj>)


来源:https://stackoverflow.com/questions/20466880/getting-compile-error-on-provided-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!