如何引用由TypeProvider创建的“生成"类型 [英] How to reference 'generated' types created by a TypeProvider

查看:100
本文介绍了如何引用由TypeProvider创建的“生成"类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为创建生成的类型的Excel文件实现类型提供程序.我的目标是能够从C#引用这些类型.

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#.

如果我使用 ed 类型创建类型提供程序,则可以使用以下语法从F#中引用它:

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.

如何在F#代码中引用参数化的生成类型?可在 GitHub

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

推荐答案

我相信问题可能是由于您如何在类型提供程序中构造类型,而不是由于使用现场的代码所致.使用类型提供程序的代码对于生成或删除的类型提供程序应该是相同的.尝试使用生成的类型提供程序正确构造类型时,发生了类似的错误.

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.

在您的情况下,您的类型派生自ExcelFileInternal,因此您需要使用BaseConstructorCall,而不仅仅是构造函数逻辑.看起来应该类似于:

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.

这篇关于如何引用由TypeProvider创建的“生成"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆