How to reference 'generated' types created by a TypeProvider

隐身守侯 提交于 2019-12-10 19:33:08

问题


I am trying to implement a type provider for Excel files that creates generated types. My goal is to be able to reference these types from C#.

If I create the type provider using erased types, I can reference it from F# using this syntax:

type DataTypesTest = ExcelFile<"tests\ExcelProvider.Tests\DataTypes.xlsx">

If I mark my types as generated. The above syntax produces this error:

A direct reference to the generated type 'ExcelFile' is not permitted. Instead, use a type definition, e.g. 'type TypeAlias = <path>'. This indicates that a type provider adds generated types to your assembly.

How do I reference a parameterized generated type in my F# code? The source for this type provider is available on GitHub


回答1:


I believe the problem may be due to how you're constructing the type in the Type Provider, not your code at the usage site. The code to use the type provider should be identical for generated or erased type providers. I had similar errors occur when trying to get the type construction correct with my generated type providers.

In your case, your type derives from ExcelFileInternal, so you need to use BaseConstructorCall, not just the constructor logic. This should look something similar to:

// add a parameterless constructor which loads the file that was used to define the schema
let ctor = ProvidedConstructor([])
ctor.BaseConstructorCall <- fun [] -> <@@ ExcelFileInternal(resolvedFilename, range) @@>
providedExcelFileType.AddMember(ctor)

Note that all constructors would need this type of change.



来源:https://stackoverflow.com/questions/22520352/how-to-reference-generated-types-created-by-a-typeprovider

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