Order of fields in a type for FileHelpers

帅比萌擦擦* 提交于 2019-12-10 22:25:20

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/6163356/order-of-fields-in-a-type-for-filehelpers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!