是否可以在F#类型提供程序中将System.Type用作静态参数? [英] Is it possible to use System.Type as static parameter in F# type provider?

查看:57
本文介绍了是否可以在F#类型提供程序中将System.Type用作静态参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将System.Type用作F#类型提供程序中的静态参数,以便我可以编写类似以下内容的东西:

I was wondering is it possible to use System.Type as the static parameter in F# type provider, so that I can write something like:

type HelperType = HelperProvider<typeof<int>>

这个想法是,有可能让类型提供程序根据某种.NET类型生成某种帮助程序类型.

The idea is, is it possible to let type provider generating some helper type based on some .NET type.

推荐答案

否,类型提供程序参数只能是原始类型(例如intstring).最好的办法是将类型名称作为字符串:

No, type provider parameters can only be of primitive types (like int and string). The best you can do is to take the type name as a string:

type HelperType = HelperProvider<"int">

这将解决原始(和标准类型)问题,但不适用于使用类型提供程序的文件(或项目)中较早定义的类型.

This will do the trick for primitive (and standard types), but it won't work for types that are defined earlier in the file (or in the project) where you're using the type provider.

据我所知,这绝对是F#团队一直在研究的东西-它将允许一些有趣的元编程应用程序.现在的主要重点是数据访问,因此并不是一个优先事项(出于好奇,您打算使用什么应用程序?)

As far as I know, this is definitely something that the F# team has been looking into - it would allow some interesting meta-programming applications. The main focus for now has been on data access, so this has not been such a priority (out o curiosity, what application do you have in mind?)

顺便说一句-将类型作为参数传递会引起一些有趣的棘手问题.例如,编译器将如何处理以下内容:

By the way - passing types as parameters can cause some interesting tricky questions. For example, how would the compiler handle something like this:

type A = MyProvider<B>
and B = MyProvider<A>

这篇关于是否可以在F#类型提供程序中将System.Type用作静态参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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