简体   繁体   English

使用F#类型提供程序实例化类型并将其显示为属性

[英]Using an F# type provider to instantiate types and present them as properties

I'd like to do the following: 我想做以下事情:

let allTypes = AllTypes (t, assemblies)

... where AllTypes is a type provider, the properties of which are instances of all types in the given array of assemblies that subclass type t . ...,其中AllTypes是类型提供程序,其属性是给定类型t AllTypes集数组中所有类型的实例。 (All of the types have a single constructor that takes no arguments.) (所有类型都有一个不带参数的构造函数。)

Is this doable using F# type providers? 使用F#类型提供程序可以做到吗? I have no experience creating my own provider, and I don't want to waste my time attempting to do this if it isn't feasible. 我没有创建自己的提供程序的经验,并且我不想浪费时间尝试这样做,如果这样做不可行。

I'd greatly appreciate any links to pages that would get me started coding this. 我非常感谢任何指向我的页面的链接,这些链接会让我开始对此进行编码。

You don't need a type provider for that; 您不需要为此提供类型提供程序; you can write that code using basic reflection: 您可以使用基本反射来编写该代码:

open System.Reflection

let allTypes (baseClass : Type) (assemblies : Assembly seq) =
    assemblies
    |> Seq.collect (fun x -> x.GetExportedTypes())
    |> Seq.filter (fun x -> baseClass.IsAssignableFrom x)
    |> Seq.collect (fun x -> x.GetConstructors())
    |> Seq.filter (fun x -> x.GetParameters().Length = 0)
    |> Seq.map (fun x -> x.Invoke([||]))

The allTypes function has this signature: Type -> Assembly seq -> obj seq . allTypes函数具有以下签名: allTypes Type -> Assembly seq -> obj seq allTypes Type -> Assembly seq -> obj seq

there's a lot of activity going on in the FSharp.Data github repo. FSharp.Data github 存储库中有很多活动。 There is a learning curve, but tuning into that repo might be useful. 有一个学习曲线,但调入该存储库可能会很有用。

Beyond that, this intro tutorial covers some of the basics, and here's a Type Provider starter pack that's been prepared by the F# open source community. 除此之外,本入门教程还介绍了一些基础知识,这是由F#开源社区准备的Type Provider入门包

The fsharp.org site, and this projects page covers a cross-section of what's going on (including type providers). fsharp.org网站以及此项目页面涵盖了正在发生的事情(包括类型提供程序)的横截面。

You could take the list that Mark suggests here and turn it into a type provider. 您可以采用Mark在这里建议的列表,然后将其变成类型提供程序。 I think an exploratory way of interacting with namespaces would be useful. 我认为探索性的与名称空间交互的方式将很有用。 Why not? 为什么不? I'd use it. 我会用它。 Please publish on GitHub if you get around to it. 如果您熟悉它,请在GitHub上发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM