How do I create an F# Type Provider that can be used from C#?

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:

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

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

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);
}

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:

  • Generated types are real .NET types that get embedded into the assembly that uses the type provider (this is what the type providers that wrap code generation tools like sqlmetal use)
  • Erased types are simulated types which are represented by some other type when the code is compiled.
  • 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">

    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# Type Providers look like a great technology, but we need help building them. Any help is appreciated.


    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.

    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 - ie they can generate huge number of "fake" types without generating excessively big assemblies.

    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 (ie using CodeDOM or Roslyn).

    链接地址: http://www.djcxy.com/p/86118.html

    上一篇: 在MSBuild中生成版本号

    下一篇: 如何创建可以从C#使用的F#Type Provider?