我可以在F#中向Type提供程序提供Type作为输入吗? [英] Can I provide a Type as an input to a Type provider in F#?

查看:76
本文介绍了我可以在F#中向Type提供程序提供Type作为输入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样做有什么陷阱吗?

您是否知道现有的代码处理我可能遇到的相同的pb?

Would you know of existing code dealing with the same pb I might encounter ?

思考

推荐答案

不幸的是,您不能将类型作为静态参数传递给类型提供程序.使用MyProvider< "first argument", 42 >传递的静态参数必须是原始类型(例如stringint等).我在文档的任何地方都看不到该列表,但是Type绝对不受支持.

Unfortunately, you cannot pass types as static parameters to a type provider. The static parameters passed using MyProvider< "first argument", 42 > have to be primitive types (like string, int and similar). I don't see the list anywhere in the documentation, but Type is definitely not supported.

执行此操作的问题是您可以将其传递给typeof<MyType>,其中MyType在与使用类型提供程序的文件相同的文件中声明,因此F#编译器必须首先编译文件,然后调用提供程序,然后继续.这听起来可能,但是对于F#团队来说可能是低优先级.

The problem with doing this is that you could pass it typeof<MyType> where MyType is declared in the same file as the file that's using the type provider, and so the F# compiler would have to first compile the first part of a file, then invoke the provider and then continue. This sounds possible, but it is probably low-priority for the F# team.

将有关类型的信息传递给类型提供程序的最好方法是给它提供一个类型名称,然后使用反射查找该类型(仅当该类型来自已编译的程序集时才有效):

The best way to pass information about type to a type provider is to give it a type name and then lookup the type using reflection (this will only work if the type is from an already compiled assembly):

type MyTest = MyTypeProvider<"System.Int32">

这篇关于我可以在F#中向Type提供程序提供Type作为输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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