FileHelpers类型中的字段顺序 [英] Order of fields in a type for FileHelpers

查看:183
本文介绍了FileHelpers类型中的字段顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我写的f#类型是:

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

我遗漏了一些基本的东西,但FileHelpers总是假定字段的顺序是

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

我在这里缺少什么?

推荐答案

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

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

这篇关于FileHelpers类型中的字段顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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