跨F#/ C#边界保留字段名 [英] Preserving Field names across the F#/C# boundary

查看:178
本文介绍了跨F#/ C#边界保留字段名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个学习的过程,我做使用F#和F#的类型提供功能的5000线的CSV文件的一些数据分析。当我使用F#中的类型提供程序生成的类型,自然是CSV文件的标题行成为该类型的字段名。

As a learning exercise, I am doing some data analysis on a 5000-line csv file using F# and F#'s type-provider functionality. When I use the type-provider-generated type in f#, naturally the header rows of the csv file become the field names of the type.

type restaurantCsv = CsvProvider<"C:\Users\Paul\Desktop\HealthDepartment\RestaurantRatings2013.csv",HasHeaders=true>

type RawInspectionData(filename : string) = 
    member this.allData = restaurantCsv.Load(filename)

    member public this.inspectionsList = this.allData.Data.ToList()

    member this.FilterByName(name : string) =
        this.allData.Data 
            |> Seq.filter (fun x -> x.EstablishmentName.Contains(name))



另外,我打电话从单元测试C#文件中的F#代码(在一个库上面的代码创建)。它工作正常。但是,从FilterByName F#的回报,字段名的信息不会被保留,当我消费IEnumberable。所以,我有代码,新的字段名称.Item1,项目2等。从元组映射:

Also, I am calling the F# code (in a library the above code creates) from a unit testing C# file. It works fine. But when I consume the IEnumberable that F# returns from FilterByName, the field name information is not preserved. So I have code which maps new field names to .Item1, Item2, etc. from the Tuple:

var inspectionsOnCafes = inspections2013.FilterByName("Cafe");
var inspections = (from row in inspectionsOnCafes
                    select new
                    {
                       inspectorID = row.Item3,
                       restaurantName = row.Item5,
                       inspectionDate = row.Rest.Item6
                    }).ToList();

我的提问有没有办法让C#认识的字段名在F# - 生成类型,这样我就不必row.Item#映射到一个易于阅读的字段名?换句话说,有没有一种方法可以让我有这样的代码:

My Question Is there a way to get C# to recognize the field names in the F#-generated type so I don't have to map row.Item# to an easy-to-read field name? In other words, is there a way I can have code like this:

inspectorID = row.InspectorID,
// etc.

请注意:我看了看的如何创建可从C#中使用的F#类型提供?但卡梅伦Taggert填入的问题,托马斯Petricek的回答在我看来是对类型供应商略有不同的方面。即使他们被点上,我不明白这一点,所以我还需要更多一点帮助。

Note: I looked at How do I create an F# Type Provider that can be used from C#? but Cameron Taggert's question and Tomas Petricek's answer seem to me to be on a slightly different aspect of type providers. Even if they are spot-on, I don't get it so I still need a little more help.

推荐答案

在F#有两种不同类型的类型提供商。更常见和简单的人被擦除型供应商。 。而更复杂的问题是生成类型提供商

In F# there are two distinct kinds of type providers. The more common and simpler ones are erased type providers. And the more complicated ones are generative type providers.

的区别是C#非常重要,因为:

The distinction is very important for C#, because:


  • 这是擦除型供应商只是编译器法宝。这是一种方式,以确保很好形成你的代码,你是正确使用垫层API。但是,这是编译时只和无类型生成。代替垫层类型用作内部表示。这种类型的提供程序是不是很方便从C#中使用,因为在C#中,您只能得到垫层类型。

  • An erased type provider is just compiler magic. It is a way to make sure your code is well-formed, that you are using the underlaying API correctly. But this is compile-time only and no types are generated. Instead the underlaying type is used as an internal representation. This kind of type provider is not very convenient to use from C#, because in C# you only get the underlaying types.

一个生成类型的提供是一个真正的代码产生步骤,在编译时。在这种情况下的类型生成并因此实际上在装配存在,可以从C#中使用。

A generative type provider is a true code generation step at compile-time. In this case types are generated and therefore actually exist in the assembly and can be used from C#.

你所描述的是什么人会从被擦除类型供应商的期望问题。而在垫层类型仅仅是一个元组,但没有很好的型象类或生成一个记录。而且你不能在C#中使用F#的名字,除非型供应商都以不同的方式写成生成型供应商。

The problem you are describing would be exactly what one would expect from an erased type provider. Whereas the underlaying type is just a tuple, but no nice type like a class or a record is generated. And you cannot use the F# names in C#, unless the type provider were to be written differently as a generative type provider.

这篇关于跨F#/ C#边界保留字段名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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