如何创建可从 C# 使用的 F# 类型提供程序? [英] How do I create an F# Type Provider that can be used from C#?

查看:11
本文介绍了如何创建可从 C# 使用的 F# 类型提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用程序集 FSharp.Data.TypeProviders 4.3.0.0 中的 F# 类型提供程序,我就能够在一个非常简单的 F# 库中创建类型.然后我就可以在不依赖程序集 FSharp.Data.TypeProviders 的情况下使用这些类型.真是太甜了!下面是一个例子:

If I use the F# Type Providers from the assembly FSharp.Data.TypeProviders 4.3.0.0, I am able to create types in a very simple F# library. I am then able to use those types without any dependency on the assembly FSharp.Data.TypeProviders. That is pretty sweet! Here is an example:

我创建了一个名为 TryTypeProviders 的 F# 库项目.我把它放在 .fs 中:

I created an F# library project called TryTypeProviders. I put this in the .fs:

module TryTypeProviders
type Northwind = Microsoft.FSharp.Data.TypeProviders.ODataService

然后我就可以使用 C# 项目中的 F# 库了:

I then am able to use the F# library from a C# project:

public static void Main()
{
    var c = new TryTypeProviders.Northwind();
    foreach (var cust in c.Customers)
        Console.WriteLine("Customer is: " + cust.ContactName);
    Console.ReadKey(true);
}

我找不到任何关于如何创建这样的类型提供程序的工作示例.FSharpx.TypeProviders 中的类型提供程序不能从 C# 访问.我的猜测是它们是擦除类型而不是生成类型.我仍然有点不清楚哪个是哪个,但它在这里定义为:

I haven't been able to find any working examples of how to create a type provider like this. The type providers in FSharpx.TypeProviders are not accessible from C#. My guess is that they are erased types and not generated types. I'm still a little fuzzy on which is which, but it is defined here as:

  1. 生成的类型是真正的 .NET 类型,它们嵌入到使用类型提供程序的程序集中(这是包装代码生成工具(如 sqlmetal 使用的类型提供程序)所使用的)
  2. 擦除类型是模拟类型,在编译代码时由其他类型表示.

MSDN 中提到的 F# 3.0 示例包中的示例/msdn.microsoft.com/en-us/library/hh361034" rel="nofollow noreferrer">教程 对我不起作用.它们构建,但当我尝试使用它们时,我会出错.

The samples from the F# 3.0 Sample Pack mentioned in the MSDN tutorial are not working for me. They build, but when I try to use them I get errors.

open Samples.FSharp.RegexTypeProvider
type PhoneNumberRegEx = CheckedRegexProvider< @"(?<AreaCode>^d{3})-(?<PhoneNumber>d{3}-d{4}$)">

open Samples.FSharp.MiniCsvProvider
type csv = MiniCsvProvider<"a.csv">

它最后一次发布是在 2011 年 3 月,我的猜测是它们还没有反映 Visual Studio 2012 附带的类型提供程序的最终版本.

It was last released in March of 2011 and my guess is that they don't yet reflect the final version of type providers that shipped with Visual Studio 2012.

F# 类型提供程序看起来是一项很棒的技术,但我们需要帮助构建它们.任何帮助表示赞赏.

F# Type Providers look like a great technology, but we need help building them. Any help is appreciated.

推荐答案

标准类型提供程序(用于 OData、LINQ to SQL 和 WSDL)与 C# 配合使用的原因是它们在幕后生成真正的 .NET 类型.这称为生成类型提供程序.事实上,它们只是调用代码生成工具,如果您以标准方式使用 C# 中的这些技术,就会调用该工具.因此,这些类型提供程序只是一些标准 .NET 工具的包装器.

The reason why standard type providers (for OData, LINQ to SQL and WSDL) work with C# is that they generate real .NET types behind the cover. This is called generative type provider. In fact, they simply call the code generation tool that would be called if you were using these technologies from C# in a standard way. So, these type providers are just wrappers over some standard .NET tools.

大多数新编写的提供程序都编写为擦除类型的提供程序.这意味着它们只生成假"类型,告诉 F# 编译器可以调用哪些成员(等),但是当编译器编译它们时,假"类型会被其他一些代码替换.这就是为什么在使用 C# 库时看不到任何类型的原因 - 编译代码中实际上不存在任何类型.

Most of the providers that are newly written are written as erasing type providers. This means that they only generate "fake" types that tell the F# compiler what members can be called (etc.) but when the compiler compiles them, the "fake" types are replaced with some other code. This is the reason why you cannot see any types when you're using the library from C# - none of the types actually exist in the compiled code.

除非您包装现有的代码生成器,否则编写擦除类型提供程序会更容易,因此大多数示例都是以这种方式编写的.擦除类型提供程序还有其他好处 - 即它们可以生成大量假"类型而不会生成过大的程序集.

Unless you're wrapping existing code-generator, it is easier to write erased type provider and so most of the examples are written in this way. Erasing type providers have other beneftis - i.e. they can generate huge number of "fake" types without generating excessively big assemblies.

无论如何,MSDN教程中有一个简短的说明提供生成的类型",其中有一些关于编写生成式提供程序的提示.但是,我希望大多数新的 F# 类型提供程序都写为擦除.它指出,您必须有一个真正的 .NET 程序集(带有生成的类型),并且 F# 帮助程序并未简化构建类型提供程序的程序集 - 因此您需要为程序集发出 IL 或生成 C#/F# 代码并编译(即使用 CodeDOM 或 Roslyn).

Anyway, there is a brief note "Providing Generated Types" in the MSDN tutorial, which has some hints on writing generative providers. However, I'd expect most of the new F# type providers to be written as erasing. It notes that you must have a real .NET assembly (with the generated types) and getting that is not simplified by the F# helpers for building type providers - so you'll need to emit the IL for the assembly or generate C#/F# code and compile that (i.e. using CodeDOM or Roslyn).

这篇关于如何创建可从 C# 使用的 F# 类型提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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