FileHelpers 类型中的字段顺序

发布于 2024-11-10 13:14:22 字数 466 浏览 0 评论 0原文

我正在使用 Filehelpers 读取一个简单的 CSV 文件 - 该文件只是一个键值对。 (string, int64)

我为此编写的 f# 类型是:

type MapVal (key:string, value:int64) =
    new()= MapVal("",0L)
    member x.Key = key
    member x.Value = value

我在这里遗漏了一些基本内容,但 FileHelpers 始终假定字段的顺序与我指定的顺序相反 - 如 Value、Key 中所示。

let dfe = new DelimitedFileEngine(typeof<MapVal>)
let recs = dfe.ReadFile(@"D:\e.dat")
recs |> Seq.length

我在这里缺少什么?

I'm reading a simple CSV file with Filehelpers - the file is just a key, value pair. (string, int64)

The f# type I've written for this is:

type MapVal (key:string, value:int64) =
    new()= MapVal("",0L)
    member x.Key = key
    member x.Value = value

I'm missing something elementary here, but FileHelpers always assumes the order of fields to be the reverse of what I've specified - as in Value, Key.

let dfe = new DelimitedFileEngine(typeof<MapVal>)
let recs = dfe.ReadFile(@"D:\e.dat")
recs |> Seq.length

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-17 13:14:22

主构造函数参数的顺序不一定决定字段在类型中出现的顺序(事实上,根据参数的使用方式,它们甚至可能不会导致生成字段)。在我看来,FileHelpers 没有提供使用属性而不是字段的方法,这一事实是不幸的。如果您想更好地控制类的物理布局,则需要显式声明字段:

type MapVal = 
    val mutable key : string
    val mutable value : int64
    new() = { key = ""; value = 0L }
    new(k, v) = { key = k; value = v }
    member x.Key = x.key
    member x.Value = x.value

The order of primary constructor parameters doesn't necessarily determine the order that fields occur within a type (in fact, depending on how the parameters are used, they may not even result in a field being generated). The fact that FileHelpers doesn't provide a way to use properties instead of fields is unforunate, in my opinion. If you want better control over the physical layout of the class, you'll need to declare the fields explicitly:

type MapVal = 
    val mutable key : string
    val mutable value : int64
    new() = { key = ""; value = 0L }
    new(k, v) = { key = k; value = v }
    member x.Key = x.key
    member x.Value = x.value
樱娆 2024-11-17 13:14:22

该库在声明中使用字段的顺序,但看起来与 F# 单词不同,在该库的最后一个稳定版本中,您可以使用 [FieldOrder(1)] 属性来提供字段的顺序。

http://teamcity.codebetter.com /viewLog.html?buildId=lastSuccessful&buildTypeId=bt66&tab=artifacts&guest=1

干杯

The library uses the order of the field in the declaration, but looks like that F# words different, in the last the last stable version of the library you can use the [FieldOrder(1)] attribute to provide the order of the fields.

http://teamcity.codebetter.com/viewLog.html?buildId=lastSuccessful&buildTypeId=bt66&tab=artifacts&guest=1

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文