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

查看:73
本文介绍了如何创建可从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);
}

我还没有找到任何有效的示例来说明如何创建这样的类型提供程序.无法从C#访问FSharpx.TypeProviders中的类型提供程序.我的猜测是它们是已擦除的类型,而不是生成的类型.我仍然不清楚哪个是哪个,但它在此处定义为:

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/zh-CN/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天全站免登陆